When find_or_create_by
returns a record with a nil id
, that suggests the find
part is failing, and then the create
part fails too with validation errors. What do you get from MyModel.find_or_create_by(email: '[email protected]').errors.full_messages
? I'm guessing you see the same uniqueness validation error as you're seeing in the web console.
Is your app using a soft-delete approach, e.g. with a gem like acts_as_paranoid
or permanent_records
? Those gems change the behavior of destroy
so that it does not issue a SQL DELETE
command but instead sets a deleted_at
column. They also hide soft-deleted records, so that may be why find_by
isn't giving you anything. If this is what you're doing, you should make sure your uniqueness validation knows to ignore soft-deleted records. How to do that depends on your soft-delete implementation, but you might find some tips here.
You might want to try straight SQL to see what's really in your database, e.g. using the Heroku psql prompt or this Ruby code: MyModel.unscoped.where(email: "[email protected]")