I'm having a problem with an :has_many => :through
association which references another :has_many => :through
association.
I have a User
->Cart
->CartItem
->Product
model setup going in my rails application. Here are the model associations:
class User < ActiveRecord::Base
has_many :purchases, :class_name => "Cart",
:dependent => :destroy,
:conditions => {:purchased => true}
has_many :items, :through => :purchases,
:readonly => true
has_many :products, :through => :purchases,
:readonly => true
end
class Cart < Activerecord::Base
belongs_to :user
has_many :items, :class_name => "CartItem",
:dependent => :delete_all
has_many :products, :through => :items
end
class CartItem < ActiveRecord::Base
belongs_to :cart
belongs_to :product
end
The idea is that a cart has many cart_items, which are just references to existing products. After a cart is marked as purchase, a user should have access to products directly via user.products
.
Anyway... I can not figure out how to setup my User
model so that the relationship is possible. I keep getting the following error:
Invalid source reflection macro :has_many :through for has_many :products,
:through => :purchases. Use :source to specify the source reflection.
I'm assumming it wants me to add a :source
attribute to the has_many :products
assoc. in the User model, but that seems silly considering the source association is named identically (and it doesn't work when I add :source => :products
anyway).
Does anyone know how I could get this to work? I'd really appreciate any suggestions!
Sorry if this question has been asked before, but I've been searching and I can't find an answer. Thanks in advance.