# Find\_or\_create\_by\_foo\_and\_bar error (duplicate entry)

**URL:** <https://www.ruby-forum.com/t/find-or-create-by-foo-and-bar-error-duplicate-entry/164458>\
**Category:** Rails\
**Created:** [April 1, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/find-or-create-by-foo-and-bar-error-duplicate-entry/164458 "2009-04-01T17:19:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![klochner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ad7895/32.png) [@klochner](https://www.ruby-forum.com/u/klochner)\
**Post date:** [April 1, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/find-or-create-by-foo-and-bar-error-duplicate-entry/164458/1 "2009-04-01T17:19:30Z")

</div>

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  
‘removed\_email\_address@domain.invalid’ 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,  
‘removed\_email\_address@domain.invalid’, ‘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.

---

<div class="post-metadata">

**Author:** ![klochner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ad7895/32.png) [@klochner](https://www.ruby-forum.com/u/klochner)\
**Post date:** [April 1, 2009, 11:32pm UTC](https://www.ruby-forum.com/t/find-or-create-by-foo-and-bar-error-duplicate-entry/164458/2 "2009-04-01T23:32:46Z")

</div>

On Wed, Apr 1, 2009 at 10:18 AM, klochner [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 D.  
[http://destiney.com/](http://destiney.com/)
