# Many-to-many relationhip

**URL:** <https://www.ruby-forum.com/t/many-to-many-relationhip/124892>\
**Category:** Rails\
**Created:** [January 3, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/many-to-many-relationhip/124892 "2008-01-03T15:59:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dazonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@dazonic](https://www.ruby-forum.com/u/dazonic)\
**Post date:** [January 3, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/many-to-many-relationhip/124892/1 "2008-01-03T15:59:01Z")

</div>

Hello friendly railers!

I’m really having trouble getting my head around database relationships.  
Is it set up in the model, or in the migration?

I’m building a basic cart for tshirts, and this is what i’ve had in  
mind:

two tables, designs and sizes.

designs sizes  
name name  
thumb  
image

then i’ll need another availability table so we can keep track of what’s  
in stock, and i’m thinking the view will look like so:

## design size in stock

Star S 25 [text inputs]  
M 8  
L 19

Shuttle S 5  
M 0  
L 15

I’m sure this is fairly straight forward, but it seems every tute on  
relationships in rails 2 uses different methods.

As far as i can understand, you really have to get things set up spot-on  
first time around, it’s a bit overwhelming.

Any help would be great, thanks!

---

<div class="post-metadata">

**Author:** ![dazonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@dazonic](https://www.ruby-forum.com/u/dazonic)\
**Post date:** [January 4, 2008, 12:01am UTC](https://www.ruby-forum.com/t/many-to-many-relationhip/124892/2 "2008-01-04T00:01:20Z")

</div>

Sounds like you want something like has\_many :through.

class Design \< ActiveRecord::Base  
has\_many :sizes, :through =\> :availability  
end

class Size \< ActiveRecord::Base  
has\_many :designs, :through =\> :availability  
end

class Availability \< ActiveRecord::Base  
#You might have to set the table name here.  
belongs\_to :design  
belongs\_to :size  
end

In the availability table you’ll have a design\_id field and a size\_id  
field  
as well as another field that’s a number, telling you how many of a  
certain  
design and size are available.

---

<div class="post-metadata">

**Author:** ![dazonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@dazonic](https://www.ruby-forum.com/u/dazonic)\
**Post date:** [January 4, 2008, 5:06am UTC](https://www.ruby-forum.com/t/many-to-many-relationhip/124892/3 "2008-01-04T05:06:51Z")

</div>

Hey thanks for that mate, it makes sense. I’ll give it a go.

Does anyone know where i can go to read right up on the database  
relationship stuff? Cheers.

---

<div class="post-metadata">

**Author:** ![dazonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@dazonic](https://www.ruby-forum.com/u/dazonic)\
**Post date:** [January 4, 2008, 5:20am UTC](https://www.ruby-forum.com/t/many-to-many-relationhip/124892/4 "2008-01-04T05:20:24Z")

</div>

Thanks lads, much appreciated!

---

<div class="post-metadata">

**Author:** ![dazonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@dazonic](https://www.ruby-forum.com/u/dazonic)\
**Post date:** [January 4, 2008, 5:17am UTC](https://www.ruby-forum.com/t/many-to-many-relationhip/124892/5 "2008-01-04T05:17:48Z")

</div>

Hi, I would recommend reading chapters 16, 17, and 18 of the “Agile Web  
Development with Rails 2ed (AWDwRv2)”. This should give you a very good  
foundation from witch to build from.  
Good luck,

-Conrad

On Jan 3, 2008 8:06 PM, Darren J.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dazonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@dazonic](https://www.ruby-forum.com/u/dazonic)\
**Post date:** [January 5, 2008, 8:39am UTC](https://www.ruby-forum.com/t/many-to-many-relationhip/124892/6 "2008-01-05T08:39:04Z")

</div>

Hi,

i set up the relationship as Ryan suggested, and it looks as if it’s  
ready to go. now i’m making a nested unordered list so we can see [and  
edit] what designs and sizes we’ve got in stock.

\*Design1  
\*S [6] \<---- text field  
\*M []  
\*L [9]  
\*XL [12]

\*Design2  
\*S [3]  
\*M [8]  
\*L [9]  
\*XL [12]

…

can this all be done in the one form? i’m not sure how to grab the  
corresponding availability, and what if it hasn’t been created yet??

i thought something like:

\<% form\_for(@availability) do |f| %\>

 \<% for design in @designs %\> - \<%=h design.name %\> 
 \<% for size in @sizes %\> - \<%= size.name %\> [TEXT FIELD FOR NUM IN STOCK]
 \<% end %\> 
\<% end %\>
\<% end %\>

all the SQL is right there in my head! still trying to grasp the whole  
object oriented thing.

Thanks

---

<div class="post-metadata">

**Author:** ![dazonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@dazonic](https://www.ruby-forum.com/u/dazonic)\
**Post date:** [January 20, 2008, 10:26am UTC](https://www.ruby-forum.com/t/many-to-many-relationhip/124892/7 "2008-01-20T10:26:26Z")

</div>

On Fri, 04 Jan 2008 05:06:51 +0100, Darren J. wrote:

> Hey thanks for that mate, it makes sense. I’ll give it a go.
> 
> Does anyone know where i can go to read right up on the database  
> relationship stuff? Cheers.

I would be more than glad to share tips by e-mail (or here); I’m working  
on a design almost exactly like what you have 🙂

-Thufir

---

<div class="post-metadata">

**Author:** ![dazonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba8739/32.png) [@dazonic](https://www.ruby-forum.com/u/dazonic)\
**Post date:** [January 6, 2008, 4:36am UTC](https://www.ruby-forum.com/t/many-to-many-relationhip/124892/8 "2008-01-06T04:36:00Z")

</div>

i can get \<%= f.text\_field :number %\> to render a text field, just  
what i want, but it doesn’t know to use both design\_id and size\_id as  
foreign keys.

\<% form\_for(@availability) do |f| %\>

 \<% for design in @designs %\> - \<%=h design.name %\> 
 \<% for size in @sizes %\> - \<%= size.name %\> \<%= f.text\_field :number %\>
 \<% end %\> 
\<% end %\>
\<% end %\>
