# Using a Many-to-Many Relationship

**URL:** <https://www.ruby-forum.com/t/using-a-many-to-many-relationship/137488>\
**Category:** Rails\
**Created:** [May 13, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/using-a-many-to-many-relationship/137488 "2008-05-13T17:10:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DVG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@DVG](https://www.ruby-forum.com/u/DVG)\
**Post date:** [May 13, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/using-a-many-to-many-relationship/137488/1 "2008-05-13T17:10:34Z")

</div>

Okay, so I have a many-to-many relationship between users and groups  
set up in my application, using a join table (users\_groups). I am able  
to reference one from the other using this relaionship. However, I’m  
not exactly sure how to relate a User to a Group in the app, other  
than hard-coding some SQL to insert a new relationship. What’s the  
proper way to do this?

---

<div class="post-metadata">

**Author:** ![DVG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@DVG](https://www.ruby-forum.com/u/DVG)\
**Post date:** [May 13, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/using-a-many-to-many-relationship/137488/2 "2008-05-13T18:59:45Z")

</div>

You should be able to append instances of one to the collection accessor  
of the other. So for instance:

fred = User.find\_by\_name(“fred”)  
godlike\_beings = Group.find\_by\_name(“godlike beings”)

godlike\_beings.users \<\< fred  
godlike\_beings.save

# or equivalently:

fred.groups \<\< godlike\_beings  
fred.save

HTH,

-Roy

---

<div class="post-metadata">

**Author:** ![DVG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@DVG](https://www.ruby-forum.com/u/DVG)\
**Post date:** [May 14, 2008, 2:36am UTC](https://www.ruby-forum.com/t/using-a-many-to-many-relationship/137488/3 "2008-05-14T02:36:54Z")

</div>

The one caveat to Roy’s response is that both the User and the Group  
must exist. It’s tempting to use that same line of coding when you’re  
creating the end points and the join table at the same time but it  
doesn’t work.

---

<div class="post-metadata">

**Author:** ![DVG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@DVG](https://www.ruby-forum.com/u/DVG)\
**Post date:** [May 15, 2008, 2:27am UTC](https://www.ruby-forum.com/t/using-a-many-to-many-relationship/137488/4 "2008-05-15T02:27:34Z")

</div>

True dat. I’ve found that to be a bit challenging myself (as evidenced  
by my questions over the last few weeks). FWIW, I took the fields\_for +  
virtual attribute approach Ryan B. explains in these railscasts:

> **[\#73 Complex Forms Part 1 - RailsCasts](http://railscasts.com/episodes/73-complex-forms-part-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

> **[\#74 Complex Forms Part 2 - RailsCasts](http://railscasts.com/episodes/74-complex-forms-part-2)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Took me a while to get my head around that, but it seems to work…
