On 17 Mar., 15:18, ball <chezb...@gmail.com> 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 Knorr
http://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 rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to