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! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> p.categories now has TWO catBikeBecause << 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 => true on the habtm (spelling?) a unique key (index) on the database --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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 On Mar 16, 4:50 pm, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 => true on the habtm (spelling?) > a unique key (index) on the database--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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">]>> p.save=> true>> p.categories=> [#<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? On Mar 17, 5:11 am, ball <chezb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On Mar 16, 4:50 pm, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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 => true on the habtm (spelling?) > > a unique key (index) on the database--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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 On Mar 17, 2:48 am, "\"Wolas!\"" <jcpen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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">]>> p.save > => true > >> p.categories > > => [#<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? > > On Mar 17, 5:11 am, ball <chezb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 > > > On Mar 16, 4:50 pm, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > 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 => true on the habtm (spelling?) > > > a unique key (index) on the database--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
rubyguy-DaQTI0RpDDMAvxtiuMwx3w@public.gmane.org
2009-Mar-17 15:12 UTC
Re: why does this happen...
On 17 Mar., 15:18, ball <chezb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.htmlI 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-/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 -~----------~----~----~----~------~----~------~--~---
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, "ruby...-DaQTI0RpDDMAvxtiuMwx3w@public.gmane.org" <ruby...-DaQTI0RpDDMAvxtiuMwx3w@public.gmane.org> wrote:> On 17 Mar., 15:18, ball <chezb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 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 -~----------~----~----~----~------~----~------~--~---
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 On Mar 17, 10:18 am, ball <chezb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > On Mar 17, 2:48 am, "\"Wolas!\"" <jcpen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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">]>> p.save > > => true > > >> p.categories > > > => [#<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? > > > On Mar 17, 5:11 am, ball <chezb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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 > > > > On Mar 16, 4:50 pm, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > 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 => true on the habtm (spelling?) > > > > a unique key (index) on the database--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
rubyguy-DaQTI0RpDDMAvxtiuMwx3w@public.gmane.org
2009-Mar-20 20:34 UTC
Re: why does this happen...
On 18 Mar., 19:23, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.I totally agree. Ball, you should really paste some code somewhere, so we can try to figure out what''s wrong here. -- 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-/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 -~----------~----~----~----~------~----~------~--~---