For example, if there is a model of this kind of relationship
class User < ApplicationRecord
has_many :lists
end
class List < ApplicationRecord
belongs_to :user
belongs_to :product
end
class Product < ApplicationRecord
has_many :lists
end
User has multiple Lists, each List references Product ... I want to bring all the products related to User ...
class User < ApplicationRecord
has_many :lists
#If this is the case, duplicate Products will be returned for the number of Lists.
# has_many :products, through: :lists
#I'll insert a distinct
has_many :products, ->{ distinct }, through: :lists
end
Very convenient (see [here] 1)
Recommended Posts