skmvasu
skmvasu

Reputation: 3108

Deleting a User record from rails console

We've some test data inside our production database and I'm trying to clean it up.

I was removing some test email addresses from the console directly.

User.where(:email=>"[email protected]").first.delete

This deleted that record.

Now when I'm trying to create another account with this same email address it says email is already taken.

I've done this several times in my dev environment, and it has worked like a charm.

I know I should never directly remove a record in prod from console, but this is an one-off scenario and I'm just curious why it is not working.

Please help me with this.

Upvotes: 2

Views: 3884

Answers (5)

TayyabZahid
TayyabZahid

Reputation: 187

Try this in console

User.where(:email => "[email protected]").delete_all 

Or if u want to delete all data

u = User.find("[email protected]")
u.delete

Upvotes: 5

Simon
Simon

Reputation: 1736

Are you using something like the Paranoia gem in production to soft-delete User records? If so, try User.with_deleted.where(:email=>"[email protected]") and if that returns a result just change the user's email address to a non-existent one.

Upvotes: 2

Pravin Mishra
Pravin Mishra

Reputation: 8434

You are deleting only first record of your User table as per your command:

User.where(:email=>"[email protected]").first.delete

I think, there are multiple entries are there.!

You should to use delete_all with User object like below:

User.where(:email=>"[email protected]").delete_all

Upvotes: 1

Bharat soni
Bharat soni

Reputation: 2786

Every time for the operation with console you have to reload the console and the insert the record new one.

And one more thing, you are using first user with email so in you user model there should be some validation with email with uniq.

Upvotes: 0

nikolayp
nikolayp

Reputation: 17919

User.where(:email=>"[email protected]").delete_all

Upvotes: 0

Related Questions