Answer the question
In order to leave comments, you need to log in
Correct association between Order, Product and User in Rails?
Friends, I have a question. I have users, everyone can make many orders , each of which will contain one product. At the moment I have done this:
class Order
belongs_to :user
belongs_to :product
end
class Product
belongs_to :category
has_many :orders
end
class User
has_many :orders
end
class User
has_many :orders
has_many :products, through: :orders
end
class Order
belongs_to :user
belongs_to :product
end
class Product
belongs_to :category
has_many :orders
has_many :users, through: :orders
end
Answer the question
In order to leave comments, you need to log in
So the same thing is written, only with an additive.
And the advantage is that AR in one request will be able to get all users who have bought, for example, a certain product.
Well, just change the naming a little, so that, kmk, it is clearer:
class User
has_many :orders
has_many :purchased_products, through: :orders, source: :product
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question