CRUD Conventions for Many to Many Relations


#1

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


#2

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.


#3

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