# An Example of a Form With Many To Many Relationships

**URL:** <https://www.ruby-forum.com/t/an-example-of-a-form-with-many-to-many-relationships/139337>\
**Category:** Rails\
**Created:** [June 3, 2008, 12:35am UTC](https://www.ruby-forum.com/t/an-example-of-a-form-with-many-to-many-relationships/139337 "2008-06-03T00:35:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 3, 2008, 12:35am UTC](https://www.ruby-forum.com/t/an-example-of-a-form-with-many-to-many-relationships/139337/1 "2008-06-03T00:35:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 3, 2008, 1:23am UTC](https://www.ruby-forum.com/t/an-example-of-a-form-with-many-to-many-relationships/139337/2 "2008-06-03T01:23:56Z")

</div>

When you’re after a many-to-many relationship in Rails, you’ve basically  
got two options. Josh S. 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](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 S. 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.

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 3, 2008, 1:30am UTC](https://www.ruby-forum.com/t/an-example-of-a-form-with-many-to-many-relationships/139337/3 "2008-06-03T01:30:30Z")

</div>

Joshua A. wrote:

> When you’re after a many-to-many relationship in Rails, you’ve basically  
> got two options. Josh S. 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.
> 
> [has\_many :through - Many-to-many Dance-off!](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 S. 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!

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 4, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/an-example-of-a-form-with-many-to-many-relationships/139337/4 "2008-06-04T15:49:01Z")

</div>

Ather S. 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 B. 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 🙂

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 3, 2008, 1:55am UTC](https://www.ruby-forum.com/t/an-example-of-a-form-with-many-to-many-relationships/139337/5 "2008-06-03T01:55:24Z")

</div>

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](http://media.railscasts.com/videos/047_two_many_to_many.mov)

– Josh

Ather S. wrote:

> Joshua A. wrote:
> 
> > When you’re after a many-to-many relationship in Rails, you’ve basically  
> > got two options. Josh S. 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.
> > 
> > [has\_many :through - Many-to-many Dance-off!](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 S. 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!
