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. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Oct 1, 10:03 pm, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Thu, Oct 2, 2008 at 7:03 AM, Me <chabgood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I tried to do shelf.cards.count and I get an error saying it cannot find shelf.id in the cards table. On Oct 2, 2:28 am, "Xavier Noria" <f...-xlncskNFVEJBDgjK7y7TUQ@public.gmane.org> wrote:> On Thu, Oct 2, 2008 at 7:03 AM, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > If I have a shelftable, cardtableand porttablehow do I go about > > inserting data into eachtablerelating one to another? > > >Insertname 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. > > ActiveRecordgives 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---