11175
(-- --)
1
Let's say I have the following models:
User
has_many :orders
has_many :items, :through => :orders
Order
belongs_to :user
has_many :items
has_many :products, :through => :items
Item
belongs_to :order
belongs_to :product
Product
has_many :items
has_many :orders, :through => items
Is it possible to do:
@user = User.find(1)
@products = @user.products
I have tried but it ddin't work. If it is impossible to do, what would
be the solution to that?
If all you need to do is get a list of all products ordered by a given
user, you could write the following method
class User << ActiveRecord::Base
def products
Product.find :all, :include => [:items], :conditions =>
["items.user_id = ? and products.id = items.product_id", self.id]
end
end
Best,
kb
11175
(-- --)
3
Therefore this means that :through can only work through one model.
Thank you Kyle for your tip.
Jordi
(Jordi)
4