Commit 1eb650d6 authored by Luke Duncalfe's avatar Luke Duncalfe

Specify dynamic inverse_of for DataFields

This change allows this code block
https://gitlab.com/gitlab-org/gitlab/-/blob/6773ed573be1ba8edee6028705502a5c47c88a87/app/models/integration.rb#L246-249
to continue working after the `Service` class was renamed `Integration`.

Previously the code was working due to Rails' ability to guess the
`inverse_of` relationship. From
https://api.rubyonrails.org/v6.1.3.1/classes/ActiveRecord/Associations/ClassMethods.html
"By default, Active Record can guess the inverse of the association
based on the name of the class."

When `Service` was renamed `Integration` this code began to fail, as
`data_fields.integration = integration` would no longer magically link
`integration.data_fields`.

This is also mentioned in
https://www.viget.com/articles/exploring-the-inverse-of-option-on-rails-model-associations/
"Obviously, this falls apart when the names do not match up. For
example, when using :class_name or :foreign_key options on your
associations. In that case, :inverse_of has to be explicitly set to the
correct names".

The `inverse_of` relationship needs to be dynamic to reflect the names
of the `has_one` associations
https://gitlab.com/gitlab-org/gitlab/-/blob/6773ed573be1ba8edee6028705502a5c47c88a87/app/models/project_services/data_fields.rb#L45-47
parent fdfb7d3c
......@@ -5,7 +5,7 @@ module Services
extend ActiveSupport::Concern
included do
belongs_to :integration, foreign_key: :service_id
belongs_to :integration, inverse_of: self.name.underscore.to_sym, foreign_key: :service_id
delegate :activated?, to: :integration, allow_nil: true
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment