Anyone have insight on this one? class Parent has_many :children end add_index "children", ["parent_id", "name"], :name => "by_parent_name", :unique => true a = Parent.find(1) a.children.find_or_create_by_name "foobar" Mysql::Error: Duplicate entry ''foobar'' for key 2: INSERT INTO `children` (`name`, `updated_at`, `parent_id`, `created_at`) VALUES ("foobar", ''2009-03-23 17:25:39'', 1, ''2009-03-23 17:25:39'') --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The name ''foobar'' exists already? On Mar 23, 1:36 pm, klochner <kloch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Anyone have insight on this one? > > class Parent > has_many :children > end > > add_index "children", ["parent_id", "name"], :name => > "by_parent_name", :unique => true > > a = Parent.find(1) > a.children.find_or_create_by_name "foobar" > > Mysql::Error: Duplicate entry ''foobar'' for key 2: INSERT INTO > `children` (`name`, `updated_at`, `parent_id`, `created_at`) VALUES > ("foobar", ''2009-03-23 17:25:39'', 1, ''2009-03-23 17:25:39'')--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
but the point is that it''s a find_or_create, and the index is on [:parent_id,:name], so it should just find the record if it''s a duplicate. Also, the following worked: Child.find_or_create_by_parent_id_and_name(1,"foobar") On Mar 23, 2:10 pm, jemminger <jemmin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The name ''foobar'' exists already? > > On Mar 23, 1:36 pm, klochner <kloch...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Anyone have insight on this one? > > > class Parent > > has_many :children > > end > > > add_index "children", ["parent_id", "name"], :name => > > "by_parent_name", :unique => true > > > a = Parent.find(1) > > a.children.find_or_create_by_name "foobar" > > > Mysql::Error: Duplicate entry ''foobar'' for key 2: INSERT INTO > > `children` (`name`, `updated_at`, `parent_id`, `created_at`) VALUES > > ("foobar", ''2009-03-23 17:25:39'', 1, ''2009-03-23 17:25:39'')--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---