# Has\_many :through 2 models

**URL:** <https://www.ruby-forum.com/t/has-many-through-2-models/135957>\
**Category:** Rails\
**Created:** [April 26, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/has-many-through-2-models/135957 "2008-04-26T23:26:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 26, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/has-many-through-2-models/135957/1 "2008-04-26T23:26:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 27, 2008, 12:30am UTC](https://www.ruby-forum.com/t/has-many-through-2-models/135957/2 "2008-04-27T00:30:06Z")

</div>

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

On Apr 26, 5:26 pm, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Let’s say I have the following models:
> 
> User  
> has\_many :orders

On Apr 26, 5:26 pm, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 27, 2008, 10:25am UTC](https://www.ruby-forum.com/t/has-many-through-2-models/135957/3 "2008-04-27T10:25:04Z")

</div>

Therefore this means that :through can only work through one model.

Thank you Kyle for your tip.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 28, 2008, 10:26pm UTC](https://www.ruby-forum.com/t/has-many-through-2-models/135957/4 "2008-04-28T22:26:36Z")

</div>

On Apr 26, 5:26 pm, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Let’s say I have the following models:  
> (…)  
> 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?

Try this:

[http://code.torchbox.com/svn/rails/plugins/nested\_has\_many\_through/](http://code.torchbox.com/svn/rails/plugins/nested_has_many_through/)

See the README for more info.
