简体   繁体   中英

Rails 3. How to setup Devise to look at the email column in a model that User inherited from?

I have a model called Person. I want User to inherit from Person.

class Person < ActiveRecord::Base
  belongs_to :organization
  validates :email, :presence => true, :uniqueness => true
end

class User < Person

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :lockable

  # for some reason I HAVE TO user attr_accessor on the following attributes or
  # else I would get noMethodError or something like that, I'll fix this later
  attr_accessor :username, :encrypted_password, :locked_at, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :sign_in_count

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :username, :password, :password_confirmation, :remember_me,
    :first_name, :last_name, :mobile_phone, :phone_one, :phone_two, :organization_id

  validates :username, :presence => true
end

Now I'm installing Devise and it wants to create the users table with an email column. But I don't want that email column because User is supposed to inherit email from Person.

I want the User model inherit from the People model. People already has an email attribute, so User doesn't need it anymore.

How can I make Devise look at the email column on the people table instead of the users table for the lockable module and whenever Devise needs to send an email ?

Can I just remove the email column from the migration?

To make it look for the person email you would do User.email and just remove the email from devise's user table migration.

You will also want to remove the email validations https://github.com/plataformatec/devise/blob/master/lib/devise/models/validatable.rb#L24

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM