# Problem with through

**URL:** <https://www.ruby-forum.com/t/problem-with-through/108635>\
**Category:** Rails\
**Created:** [July 5, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/problem-with-through/108635 "2007-07-05T15:05:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![michau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@michau](https://www.ruby-forum.com/u/michau)\
**Post date:** [July 5, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/problem-with-through/108635/1 "2007-07-05T15:05:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![michau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@michau](https://www.ruby-forum.com/u/michau)\
**Post date:** [July 5, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/problem-with-through/108635/2 "2007-07-05T15:52:42Z")

</div>

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](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![michau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@michau](https://www.ruby-forum.com/u/michau)\
**Post date:** [July 6, 2007, 3:23pm UTC](https://www.ruby-forum.com/t/problem-with-through/108635/3 "2007-07-06T15:23:34Z")

</div>

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
