active record multiple table insert

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.

Well, there are a few approaches you could take to do this.

1. Controller actions defining a logical "function"
Make the process of inserting all the records, externally, as a
controller action. Allowing you to do them sequentially.

2. Use ActiveRecord hooks
Take a look at the hooks like #after_save, #before_save, etc.

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 = => 's')

card1 = => 'c1')
card2 = => 'c2')

1.times {}
2.times {}

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

I tried to do and I get an error saying it cannot
find in the cards table.