Forum: Ruby on Rails mysql error on scoped find_or_create_by_attr

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
06d2325506af65508893e873ca193d6a?d=identicon&s=25 klochner (Guest)
on 2009-03-23 18:36
(Received via mailing list)
Anyone have insight on this one?

class Parent
  has_many :children

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')
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2009-03-23 19:12
(Received via mailing list)
The name 'foobar' exists already?
06d2325506af65508893e873ca193d6a?d=identicon&s=25 klochner (Guest)
on 2009-03-23 19:16
(Received via mailing list)
but the point is that it's a find_or_create, and the index is on
so it should just find the record if it's a duplicate.

Also, the following worked:

This topic is locked and can not be replied to.