I'm a newbie and I just showed my code to an expert, that told me I shouldn't use has_many
to filter my variables, but scopes
.
I have three models : User, Product and Ownership.
So here is my code in app/models/user.rb :
class User
has_many :ownerships, foreign_key: "offerer_id",
dependent: :destroy
has_many :owned_products, through: :ownerships,
source: :product
has_many :future_ownerships, -> { where owning_date: nil, giving_date: nil },
class_name: "Ownership",
foreign_key: "offerer_id"
has_many :wanted_products, through: :future_ownerships,
source: :product
end
So I deleted the has_many :future_ownerships
and has_many :wanted_products
, and created a scope in app/models/ownership.rb :
class Ownership
scope :future, -> { where owning_date: nil, giving_date: nil }
end
Now I can find the future ownerships doing this : user.ownerships.future
. But what I don't know, is how to retrieve the wanted products ? How can I make a scope in my app/models/product.rb to be able to type something like that :
user.owned_products.wanted