why does this happen...

Quick question, using ActiveRecord,

  catBike = Category.new(:name => "Bikes")   catBike.save

  p = Product.new(:name => "Bike", :price => 900)   p.categories << catBike   p.save

p.categories now has TWO catBike

however, if I move the save before adding the categories it is fine.

I am curious why this happens. Thanks!

p.categories now has TWO catBike

Because << does not ask questions. It just adds a catBike. If you already had one, now you have two.

You might need some combination of these:

   a unique key (index) on the database

It didn't exist before hand. ActiveRecord is triggering two inserts. I have included a full example and the SQL

  catBike = Category.new(:name => "Bikes")   catBike.save

  p = Product.new(:name => "Cannondale Bike", :price => 1000)   p.save   p.categories << catBike # This works great.

  p = Product.new(:name => "Trek Bike", :price => 900)   p.categories << catBike # this causes two entriens to be created   p.save

Category Create (1.0ms) INSERT INTO `categories` (`name`) VALUES ('Bikes') Product Create (0.0ms) INSERT INTO `products` (`name`, `price`) VALUES('Cannondale Bike', 1000) SQL (0.0ms) COMMIT SQL (0.0ms) BEGIN categories_products Columns (10.0ms) SHOW FIELDS FROM `categories_products` SQL (1.0ms) INSERT INTO `categories_products` (`product_id`, `category_id`) VALUES (1, 1) SQL (0.0ms) COMMIT SQL (0.0ms) BEGIN SQL (0.0ms) COMMIT SQL (0.0ms) BEGIN Product Create (1.0ms) INSERT INTO `products` (`name`, `price`) VALUES('Trek Bike', 900) categories_products Columns (7.0ms) SHOW FIELDS FROM `categories_products` SQL (1.0ms) INSERT INTO `categories_products` (`product_id`, `category_id`) VALUES (2, 1) categories_products Columns (7.0ms) SHOW FIELDS FROM `categories_products` SQL (0.0ms) INSERT INTO `categories_products` (`product_id`, `category_id`) VALUES (2, 1) SQL (0.0ms) COMMIT

I replicated 2 scenareos and neither of them do what you have described:

has and belongs to many scenario

== models

class Product   has_and_belongs_to_many :categories end

class Category   has_and_belongs_to_many :products end

== db

create_table "categories", :force => true do |t|     t.string "name"   end

  create_table "categories_products", :id => false, :force => true do | t>     t.integer "product_id"     t.integer "category_id"   end

  create_table "products", :force => true do |t|     t.string "name"     t.integer "price"   end

== test

p = Product.new :name => "cool bike", :price => 123

=> #<Product id: nil, name: "cool bike", price: 123>

p.categories << c

=> [#<Category id: 1, name: "bikes">]


=> true


=> [#<Category id: 1, name: "bikes">]

Same thing with has_many :through. which leads me to believe you havent set your relationships right. Could you show us the mdoel and table code plz?

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. :slight_smile:

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.

RAFB apparently drops files after 24 hours - any chance you could put those examples up someplace (gist or pastie, for instance) with a little more persistence?

Without seeing the code, it's hard to even guess what's going on. But I seem to recall seeing similar behavior when I had code saving an associated record in before_save, which then got picked up again by the association save callbacks.

--Matt Jones

I totally agree. Ball, you should really paste some code somewhere, so we can try to figure out what's wrong here.