An Example of a Form With Many To Many Relationships

I wished to look at an example of a form that does the following:

[table A] [table A_B] [table B]
a_id A_B_id b_id
nameA a_id nameB
              b_id

The above table basically allows for a many to many relationship as
follows:

A<<-->A_B<-->>B

Is there a tutorial or book or example out there that deals with
creating a form as above? I am planning on using this fields_for
technique to embed forms of related models into one another.

I ask because I just created a form where there are is a one to one
relationship but I dont know how I will go about adding models with many
to many relationships (as above) or an example of such a form would be
great. I have run across multiple one to many relationship tutorials
like the category assigned to a form etc.

When you're after a many-to-many relationship in Rails, you've basically
got two options. Josh Susser wrote a great article a while back on the
two options with a sort of side-by-side comparison to help people
wondering which method they should use.

http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off

IMO, a has_and_belongs_to_many association is perfect when I just need a
straight many-to-many association and no other data regarding that
association. has_many :through comes in really handy when I do need to
store some sort of data regarding an association. Eg. a many-to-many
relationship between Magazine and Reader would be handled :through =>
:subscriptions. The benefit of this is that since Subscription is a
separate model and not strictly a join table, I can store data in there
like subscription_date, renewal_date, amount_paid, etc.

I said all of that to say this: the way you build your form will sort of
hinge on which of these methods you decide to use to build your m-2-m
relationship.

Let me know what you decide.

-- Josh

Ather Shiraz wrote:

Joshua Abbott wrote:

When you're after a many-to-many relationship in Rails, you've basically
got two options. Josh Susser wrote a great article a while back on the
two options with a sort of side-by-side comparison to help people
wondering which method they should use.

http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off

IMO, a has_and_belongs_to_many association is perfect when I just need a
straight many-to-many association and no other data regarding that
association. has_many :through comes in really handy when I do need to
store some sort of data regarding an association. Eg. a many-to-many
relationship between Magazine and Reader would be handled :through =>
:subscriptions. The benefit of this is that since Subscription is a
separate model and not strictly a join table, I can store data in there
like subscription_date, renewal_date, amount_paid, etc.

I said all of that to say this: the way you build your form will sort of
hinge on which of these methods you decide to use to build your m-2-m
relationship.

Let me know what you decide.

-- Josh

Ather Shiraz wrote:

I wished to look at an example of a form that does the following:

[table A] [table A_B] [table B]
a_id A_B_id b_id
nameA a_id nameB
              b_id

The above table basically allows for a many to many relationship as
follows:

A<<-->A_B<-->>B

Is there a tutorial or book or example out there that deals with
creating a form as above? I am planning on using this fields_for
technique to embed forms of related models into one another.

I ask because I just created a form where there are is a one to one
relationship but I dont know how I will go about adding models with many
to many relationships (as above) or an example of such a form would be
great. I have run across multiple one to many relationship tutorials
like the category assigned to a form etc.

The association I have does have fields which require setting, meaning
we do need to set some fields in the intermediate (middle) table.

I am setting up my form such that it is centered around A's MVC (or
controller).

Currently I am using this method outlined on page 492 of the Agile Web
Dev with Rails 2006 edition (if anyone has a more recent e book I would
love to have it please in exchange for other ROR, Java/J2ee etc. books).

Thanks!

Well, the fact that you need additional fields in your join table sort
of decides that you're headed down the has_many :through path. Ryan
Bates screencast shows how to set up your models and migration to get
started down that path in this episode. The first half is setting up for
habtm, and even though you're not looking to do that, I would suggest
watching the whole thing.

http://media.railscasts.com/videos/047_two_many_to_many.mov

-- Josh

Ather Shiraz wrote:

Ather Shiraz wrote:

I wished to look at an example of a form that does the following:

[table A] [table A_B] [table B]
a_id A_B_id b_id
nameA a_id nameB
              b_id

The above table basically allows for a many to many relationship as
follows:

A<<-->A_B<-->>B

Is there a tutorial or book or example out there that deals with
creating a form as above? I am planning on using this fields_for
technique to embed forms of related models into one another.

I ask because I just created a form where there are is a one to one
relationship but I dont know how I will go about adding models with many
to many relationships (as above) or an example of such a form would be
great. I have run across multiple one to many relationship tutorials
like the category assigned to a form etc.

Ryan Bates has a good recipe for this in the new Advanced Rails Recipes
book. I've used a technique very similar to his, though mine uses a
before_validation callback to process the associated model attributes
instead of overriding the attribute setter (that won't make sense until
you read his recipe). It's on my list to blog about, but not high on
the list. Get the book if you want to know what to do for now :slight_smile: