# Has\_many with 3 models

**URL:** <https://www.ruby-forum.com/t/has-many-with-3-models/182867>\
**Category:** Rails\
**Created:** [February 4, 2010, 3:21am UTC](https://www.ruby-forum.com/t/has-many-with-3-models/182867 "2010-02-04T03:21:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 4, 2010, 3:21am UTC](https://www.ruby-forum.com/t/has-many-with-3-models/182867/1 "2010-02-04T03:21:39Z")

</div>

Hi,

I have the following 3 models. User , Relationship\_type, and Link.

```
 User

```

has\_many :relationship\_types

```
 Relationship_type

```

belongs\_to :users  
has\_many :links

```
  Link

```

belongs\_to :relationship\_type

The Link model has a column :partner. I’d like to find all partners  
of a user so I’d like to say in User model

has\_many :partners  
:through =\> :relationship\_type, :link

but that creates errors. How can I do this to be able to say  
something like userA.partners .

Similarly, how can I do the reverse, getting all users that link to a  
partner , e.g. partnerA.users?

In addition, how can I get to User from Link, to say something like  
linkA.user ?

Thanks.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 4, 2010, 3:49am UTC](https://www.ruby-forum.com/t/has-many-with-3-models/182867/2 "2010-02-04T03:49:09Z")

</div>

BTW, the partners are of class User. So from users to partners, it’s  
many-to-many.  
From users to relationship\_types, or from relationship\_types to links,  
it’s one-to-many.  
From links to partners, it’s one-to-one.

Thanks.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 4, 2010, 4:52am UTC](https://www.ruby-forum.com/t/has-many-with-3-models/182867/3 "2010-02-04T04:52:11Z")

</div>

Learn by Doing wrote:

> Hi,
> 
> I have the following 3 models. User , Relationship\_type, and Link.
> 
> ```
> User
> 
> ```
> 
> has\_many :relationship\_types
> 
> ```
> Relationship_type
> 
> ```

Should be RelationshipType.

> belongs\_to :users  
> Should be :user.  
> has\_many :links
> 
> ```
> Link
> 
> ```
> 
> belongs\_to :relationship\_type
> 
> The Link model has a column :partner. I’d like to find all partners  
> of a user so I’d like to say in User model
> 
> has\_many :partners  
> :through =\> :relationship\_type, :link
> 
> but that creates errors. How can I do this to be able to say  
> something like userA.partners

You may want to look at the nested\_has\_many\_through plugin.

> Similarly, how can I do the reverse, getting all users that link to a  
> partner , e.g. partnerA.users?
> 
> In addition, how can I get to User from Link, to say something like  
> linkA.user ?

link.relationship\_type.user  
or you can use :through.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

> Thanks.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 4, 2010, 7:52pm UTC](https://www.ruby-forum.com/t/has-many-with-3-models/182867/4 "2010-02-04T19:52:05Z")

</div>

Marnen!

Thank you so much. I’ll take a look at the nested\_has\_many\_through  
plugin. I needed your guidance: googling for something like that  
without using the technical term “nested” was fruitless.

Vincent.
