2
votes

I'm using Devise gem for a RoR project and I got an issue on it. I've a single table inheritance User which should have an email and password, but I also have a customer model which inherit from the User model and this model will never have a password and might or not have an email. That email is only a field for customer's profile information.

def User < ActiveRecord::Base
 devise ..., :validatable
end

def Customer < User
 def email_required?
    false
  end

  def password_required?
    false
  end
end 

My problem is when I'm creating my first customer with an empty email field, it create a customer with an empty email but for the second customer I got an error:

PG::Error: ERROR: duplicate key value violates unique constraint "index_users_on_email" DETAIL: Key (email)=() already exists.

It tries to create a other customer but with the same empty email. By default Devise sets an uniqueness validation on email and a default value "".

Thanks

1

1 Answers

1
votes

For that particular use case, the only option you have is to use a different table for the Customer model, since the index on the email attribute applies also to customers, which is why rails is giving you the error.

The other (but not recommended) solutions is to remove the index from the users table, but that would negatively affect performance.