# CRUD Conventions for Many to Many Relations

**URL:** <https://www.ruby-forum.com/t/crud-conventions-for-many-to-many-relations/153682>\
**Category:** Rails\
**Created:** [November 20, 2008, 2:56am UTC](https://www.ruby-forum.com/t/crud-conventions-for-many-to-many-relations/153682 "2008-11-20T02:56:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eli\_Gibson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a8b319/32.png) [@Eli\_Gibson](https://www.ruby-forum.com/u/Eli_Gibson)\
**Post date:** [November 20, 2008, 2:56am UTC](https://www.ruby-forum.com/t/crud-conventions-for-many-to-many-relations/153682/1 "2008-11-20T02:56:02Z")

</div>

I am curious is there is a convention for the the following in CRUD:

I have users and groups. Each group can have many users, and each user  
can have many groups.  
These are related through a has\_many :through pattern, with the  
connecting model being membership.

The most common uses of these data are to get a list of groups for one  
user, or a list of users for one group, or to check if two users are  
in the same group.

What would be the best way to set up the controllers so that these  
uses are easy and intuitive. Or, what setup would make it easiest for  
the next developer to grok the system when I get hit by a bus.

Regards,

Eli

---

<div class="post-metadata">

**Author:** ![Eli\_Gibson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a8b319/32.png) [@Eli\_Gibson](https://www.ruby-forum.com/u/Eli_Gibson)\
**Post date:** [November 20, 2008, 4:00am UTC](https://www.ruby-forum.com/t/crud-conventions-for-many-to-many-relations/153682/2 "2008-11-20T04:00:26Z")

</div>

Eli Gibson wrote:

> I am curious is there is a convention for the the following in CRUD:
> 
> I have users and groups. Each group can have many users, and each user  
> can have many groups.  
> These are related through a has\_many :through pattern, with the  
> connecting model being membership.
> 
> The most common uses of these data are to get a list of groups for one  
> user, or a list of users for one group, or to check if two users are  
> in the same group.
> 
> What would be the best way to set up the controllers so that these  
> uses are easy and intuitive. Or, what setup would make it easiest for  
> the next developer to grok the system when I get hit by a bus.
> 
> Regards,
> 
> Eli

Hi Eli,

The great thing about RESTful development is that you can set up  
multiple resources to access the same controller. So you could do  
something like:

map.resources :users, :has\_many =\> :user\_groups  
map.resources :user\_groups, :has\_many =\> :users

The being said you controller and views need to be flexible enough to  
allow for a parent record to be present or not. I would recommend taking  
a look at the resource\_controller plugin for this, it gives you all  
kinds of nifty methods for dealing with this.

Then to make all of this work I would do something like this in your  
models:

has\_many :user\_groups, :through =\> :memberships

Now your interface can be set up however you want, and flexible enough  
to access the data from both ends:

users/2/user\_groups  
user\_groups/2/users

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Eli\_Gibson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a8b319/32.png) [@Eli\_Gibson](https://www.ruby-forum.com/u/Eli_Gibson)\
**Post date:** [November 20, 2008, 7:35am UTC](https://www.ruby-forum.com/t/crud-conventions-for-many-to-many-relations/153682/3 "2008-11-20T07:35:01Z")

</div>

Wow,

You just opened up a whole new world for me. Thank you.

On Nov 19, 7:00 pm, Ben J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
