Many-to-many relationhip

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.

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.

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

Thanks lads, much appreciated!

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| %> <ul>   <% for design in @designs %>   <li>   <%=h design.name %>     <ul>       <% for size in @sizes %>       <li><%= size.name %> [TEXT FIELD FOR NUM IN STOCK]</li>       <% end %>     </ul>   </li> <% end %> </ul> <% end %>

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

Thanks

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| %> <ul>   <% for design in @designs %>   <li>   <%=h design.name %>     <ul>       <% for size in @sizes %>       <li><%= size.name %> <%= f.text_field :number %></li>       <% end %>     </ul>   </li> <% end %> </ul> <% end %>

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 :slight_smile:

-Thufir