I’ve been trying to mentally absorb how includes and associations work when trying to query a has_many :through
association.
My understanding at this point:
The symbol in the includes()
is the same as the association name on whatever model you have. For example:
class Account < ActiveRecord::Base
has_many :account_destinations
has_many :destinations, through: :account_destinations
end
class AccountDestination < ActiveRecord::Base
has_one :destination
# has an active_destination attribute
end
class Destination < ActiveRecord::Base
# has several attributes that we might want to use to filter account_destinations on
end
The association used in includes
for account_destinations.includes
is :destination
, because AccountDestination
has_one
:destination
. Meanwhile, in the where
clause, the hash key for specifying what values to match in Destination
refers to the table name, or destinations
.
class Account
.
.
def some_method
account_destinations.includes(:destination)
.where
.not(destinations: { name: %w(some list of names) } )
end
end