Many-to-many relationhip

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!

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.

Thanks lads, much appreciated!

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.
[email protected]

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

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

-Thufir

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 %>