:inverse_of
option.12 foreign_key: "journal_ref_id",
13 optional: true
14
15 belongs_to :client,16 class_name: "Eve::Character",
17 primary_key: "character_id",
18 foreign_key: "client_id",
:inverse_of
option. 7
8 belongs_to :type, class_name: "Eve::Type", optional: true
9
10 belongs_to :wallet_journal,11 primary_key: "wallet_journal_id",
12 foreign_key: "journal_ref_id",
13 optional: true
:inverse_of
option. 6
7 belongs_to :type, optional: true
8
9 belongs_to :dogma_attribute,10 primary_key: "attribute_id",
11 foreign_key: "attribute_id",
12 optional: true
:inverse_of
option. 2
3module Eve
4 class RequiredItem < ApplicationRecord
5 belongs_to :loyalty_store_offer, 6 foreign_key: "eve_loyalty_store_offer_id"
7
8 belongs_to :type,
:inverse_of
option.14
15 has_many :types # rubocop:disable Rails/HasManyOrHasOneDependent
16
17 has_many :subgroups, # rubocop:disable Rails/HasManyOrHasOneDependent18 class_name: "Eve::MarketGroup",
19 foreign_key: "parent_group_id"
20
In some usages of has_one
, has_many
and belongs_to
associations, Active Record can't automatically determine the inverse association because of a scope or the options used.
:inverse_of
must be manually specified for Active Record to use the associated object in memory, or set to false
to opt-out. Note that setting nil
does not stop Active Record from trying to determine the inverse automatically, and is not considered a valid value for this.
class Blog < ApplicationRecord
has_many :posts, -> { order(published_at: :desc) }
end
class Blog < ApplicationRecord
with_options inverse_of: :blog do
has_many :posts, -> { order(published_at: :desc) }
end
end
In the blog with order scope example above, traversing the Blog
's association in both directions with blog.posts.first.blog
would cause the blog to be loaded from the database twice.