On Thu, Oct 2, 2008 at 7:03 AM, Me <[EMAIL PROTECTED]> wrote:
>
> If I have a shelf table, card table and port table how do I go about
> inserting data into each table relating one to another?
>
> Insert name of the shelf and # of cards the shelf has, and the name of
> each card and other data, and how many ports on each card?
>
> Shelf has many cards.
> Card has many ports.

Active Record gives you API to build, save, and retrieve this stuff at
a high-level. In particular it handles recursive save and FK
assignment for you:

shelf = Shelf.new(:name => 's')

card1 = shelf.cards.build(:name => 'c1')
card2 = shelf.cards.build(:name => 'c2')

1.times {card1.ports.build}
2.times {card2.ports.build}

shelf.save

puts "dumping shelf #{shelf.name}, which has #{shelf.cards.count} cards"
for card in shelf.cards
  puts "dumping card #{card.name}, which has #{card.ports.count} ports"
  for port in card.ports
    puts "shelf #{shelf.id} - card #{card.id} - port #{port.id}"
  end
end

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to