Thanks David, I poked around in the debugger a bit after your suggestion, and honestly, I don't understand Ruby code well enough to make sense of it all. Not to mention, I kept running into >> No sourcefile available for (eval)
However, what I did find was that the << operator, did not issue the insert command in the "double" case. The save command after the fact seemed to issue the two inserts. Now this confuses me completely. So, if anyone can explain why/how this could happen, that would be great. On Mar 17, 8:12 am, "[email protected]" <[email protected]> wrote: > On 17 Mar., 15:18, ball <[email protected]> wrote: > > > Thanks. I am sure it is something I am missing. I am trying to learn > > about SQL and Rails. > > Here is the script I am using. I am really just trying to create a > > script that explores all of the database relationships and how to > > implement them in Rails. > > > The code:http://rafb.net/p/iUgqjX46.html > > > The output and SQL:http://rafb.net/p/tpvlVc44.html > > I believe it's because the << method both append the specified object > to the collection (in your case, a Category to a Products categories > collection) AND saves it afterwards. So saving the product twice might > cause the category to be added twice. At least that's what I think. So > in your case I'ld do like this: > > cat_bike = Category.new(:name => "Bikes") > cat_bike.save > > p = Product.new(:name => "Bike", :price => 900) > p.save > p.categories << cat_bike > > To avoid duplication in your categories table. Hope that was > helpful. :) > > -- > Cheers, > David Knorrhttp://twitter.com/rubyguy --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

