views:

187

answers:

1

I have a user model on which I check to make sure that the email address supplied is unique:

validates_uniqueness_of :email

This model acts as paranoid. On destroy, I need to remove the email address so that if the user wants to re-register, they can. For this, I have the following:

before_destroy :remove_email

def remove_email
  self.email = "[deleted]"
  save(false)
end

The method is called at the appropriate time, but the save method throws the following error:

ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry '[deleted]' for key 2: UPDATE `users` SET `email` = '[deleted]', `updated_at` = '2009-07-16 12:29:05' WHERE `id` = 53

So it looks like the validation is still being run at some level. Is there any way of getting around this?

+2  A: 

I found the problem: I had a unique index on email. If you get this error, check your schema.db for unique indexes. The clue is that Mysql is throwing the exception, not rails.

As the saying goes: "SELECT isn't broken".

Mr. Matt