Forum: Ruby on Rails find_or_create_by_foo_and_bar error (duplicate entry)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
06d2325506af65508893e873ca193d6a?d=identicon&s=25 klochner (Guest)
on 2009-04-01 17:19
(Received via mailing list)
I'm throwing sql errors on a User.find_or_create_by_foo_and_email
call.

I created an index with the following:
add_index "users", ["foo_id", "email"], :name =>
"by_foo_email", :unique => true

Then in a class foo instance method:

user = User.find_or_create_by_foo_id_and_email
(self.id,some_email_address)

and I get this error:
ActiveRecord::StatementInvalid (Mysql::Error: Duplicate entry
'joe@yahoo.com' for key 2: : INSERT INTO `users` (`name`,
`unsubscribed`, `updated_at`, `unsubscribed_date`, `user_id`,
`bounce_date`, `foo_id`, `email`, `created_at`, `bounced`) VALUES
(NULL, 0, '2009-03-31 16:04:51', NULL, NULL, NULL, 11,
'joe@yahoo.com', '2009-03-31 16:04:51', 0)

Isn't find_or_create_by() supposed to only create if it can't find?

caveat - I'm running this with workling/starling.
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-04-01 23:32
(Received via mailing list)
On Wed, Apr 1, 2009 at 10:18 AM, klochner <klochner@gmail.com> wrote:
> user = User.find_or_create_by_foo_id_and_email
> Isn't find_or_create_by() supposed to only create if it can't find?
I think that's just for the primary key.



--
Greg Donald
http://destiney.com/
This topic is locked and can not be replied to.