# Has\_many :through

**URL:** <https://www.ruby-forum.com/t/has-many-through/140748>\
**Category:** Rails\
**Created:** [June 18, 2008, 8:14am UTC](https://www.ruby-forum.com/t/has-many-through/140748 "2008-06-18T08:14:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 18, 2008, 8:14am UTC](https://www.ruby-forum.com/t/has-many-through/140748/1 "2008-06-18T08:14:07Z")

</div>

Hi  
I have the models  
1.Group  
has\_many :user\_groups  
has\_many :contacts, :through =\> :user\_groups  
2.Contacts  
has\_many :user\_groups  
has\_many :groups, :through =\> :user\_groups  
3.UserGroup  
belongs\_to :group  
belongs\_to :contact

```
     Now i have a group_id.I have to find the contact (he is the

```

manager (group\_user\_type\_id=4 in user\_groups)…How can I do that?

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 18, 2008, 8:50am UTC](https://www.ruby-forum.com/t/has-many-through/140748/2 "2008-06-18T08:50:20Z")

</div>

Sijo Kg wrote:

> Hi  
> I have the models  
> 1.Group  
> has\_many :user\_groups  
> has\_many :contacts, :through =\> :user\_groups  
> 2.Contacts  
> has\_many :user\_groups  
> has\_many :groups, :through =\> :user\_groups  
> 3.UserGroup  
> belongs\_to :group  
> belongs\_to :contact
> 
> ```
> Now i have a group_id.I have to find the contact (he is the
> 
> ```
> 
> manager (group\_user\_type\_id=4 in user\_groups)…How can I do that?
> 
> Thanks in advance  
> Sijo

Unless you are recording other information about the relationship  
between Groups and Contacts, then an HABTM relationship is probably a  
better fit:

class Group  
has\_and\_belongs\_to\_many :contacts  
end

class Contact  
has\_and\_belongs\_to\_many :groups  
end

and create your join table (don’t forget to drop the id column).

Then you can do things like:

Group.find(:first).contacts -\> Returns an array of Contacts belonging  
to that group

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 18, 2008, 8:58am UTC](https://www.ruby-forum.com/t/has-many-through/140748/3 "2008-06-18T08:58:29Z")

</div>

Hi  
Thanks for your reply.Here in junction table user\_groups I have the  
additionl storing like  
id integer not null default nextval(‘user\_groups\_id\_seq’::regclass)  
contact\_id | integer |  
group\_id | integer |  
group\_user\_type\_id | integer |

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 19, 2008, 12:35am UTC](https://www.ruby-forum.com/t/has-many-through/140748/4 "2008-06-19T00:35:27Z")

</div>

Sijo Kg wrote:

> Hi  
> Thanks for your reply.Here in junction table user\_groups I have the  
> additionl storing like  
> id integer not null default nextval(‘user\_groups\_id\_seq’::regclass)  
> contact\_id | integer |  
> group\_id | integer |  
> group\_user\_type\_id | integer |
> 
> Sijo

Ahhh - I see. You are not only recording the contacts belonging to a  
group, but also different _kinds_ of groups.

May I suggest you actually create another model to handle group\_types,  
something like this:

class Contact  
has\_and\_belongs\_to\_many :groups  
has\_many :grouptypes, :through =\> :groups  
end

class Group  
has\_and\_belongs\_to\_many :contacts  
belongs\_to :grouptype  
end

class GroupType

# Has a text field called “name”

has\_many :groups  
has\_many :contacts, :through =\> :groups  
end

Grouptype.find\_by\_name(“Manager”).contacts

etc.

Am I on the right track here?
