Problem with through

Has anyone ever had problems with :through relationship?
I have set it properly, according to the book, but getting response
from the browser

“Could not find the association :line_items in model Product”

and the relationship set in Product model is:
has_many :orders, :through => :line_items

Order model looks like:
has_many :line_items

and LineItem
belongs_to :product
belongs_to :order

I want to find orders which contain product with an id = 3
I am doing it through a method

def who_bought
@product = Product.find(params[:id])
@orders = @product.orders
end

and then I have an rxml template to iterate over the orders and print
them onto the screen.
Help please!

michau wrote:

Has anyone ever had problems with :through relationship?
I have set it properly, according to the book, but getting response
from the browser

“Could not find the association :line_items in model Product”

and the relationship set in Product model is:
has_many :orders, :through => :line_items

Order model looks like:
has_many :line_items

and LineItem
belongs_to :product
belongs_to :order

I want to find orders which contain product with an id = 3
I am doing it through a method

def who_bought
@product = Product.find(params[:id])
@orders = @product.orders
end

and then I have an rxml template to iterate over the orders and print
them onto the screen.
Help please!

The error message “Could not find the association :line_items in model
Product” is pretty clear. You haven’t defined the primary line_items
association that you need to go through. Do this:

Product:
has_many :line_items, :dependent => :destroy
has_many :orders, :through => :line_items

Order:
has_many :line_items, :dependent => :destroy
has_many :products, :through => :line_items

LineItem
belongs_to :product
belongs_to :order


Josh S.
http://blog.hasmanythrough.com

Wow, thanks,
that helped. Im totally new in RoR and as much as it’s all features
seems to be helpful, they’re also confusing sometimes.

Cheers