我正在使用两个用户模型设计 - 员工模型和雇主模型。
我在尝试让密码重置链接与两个模型一起使用时遇到问题。 目前设计狂放地将密码重置链接发送给属于Employee模型的用户,并且找不到属于雇主模型的用户。
有关如何配置设计以使用两种模型的任何建议。
这两个模型是:
class Employer < ActiveRecord::Base
#Twilo
ACCOUNT_SID = '#####'
ACCOUNT_TOKEN = '####'
# Include default devise modules. Others available are:
# :token_authenticatable,
# :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :timeoutable, :lockable
acts_as_token_authenticatable
before_create :create_twilio_subaccount
after_create :rent_number
validates :first_name, :presence => {:message => 'Cannot be blank'}
validates :last_name, :presence => {:message => 'Cannot be blank'}
validates_acceptance_of :terms
validates_format_of :first_name, :with => /\A[a-zA-Z ]*\z/i,
:message => "can only contain letters and spaces."
validates_format_of :last_name, :with => /\A[a-zA-Z ]*\z/i,
:message => "can only contain letters and spaces."
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :current_password, :remember_me, :first_name, :terms,
:last_name, :tax_file_number, :phone_number, :address, :state_id, :post_code, :location, :avatar, :business, :latitude, :longitude, :paid_employees, :sms_phone_number
attr_accessor :current_password
# attr_accessible :title, :body
has_one :setting, :dependent => :destroy
has_many :employees, :dependent => :destroy
has_many :message_threads, :dependent => :destroy
has_many :messages, :dependent => :destroy
has_many :schedules, :dependent => :destroy
has_many :tasks, :dependent => :destroy
has_many :announcements
has_many :keychains
has_one :quickbook
mount_uploader :avatar, AvatarUploader
geocoded_by :full_address
after_validation :geocode
scope :recent_signups, where(:created_at => Date.today..28.days.from_now.to_date)
class << self # Class methods
end
def full_address
"#{address} #{post_code}"
end
def get_full_name
"#{first_name} #{last_name}"
end
private
def create_twilio_subaccount
@client = Twilio::REST::Client.new(ACCOUNT_SID, ACCOUNT_TOKEN)
@subaccount = @client.accounts.create({:FriendlyName => self[:email]})
self.twilio_account_sid = @subaccount.sid
self.twilio_auth_token = @subaccount.auth_token
end
def rent_number
# create a new Twilio client for subaccount
@sub_account_client = Twilio::REST::Client.new(self[:twilio_account_sid], self[:twilio_auth_token])
@subaccount = @sub_account_client.account
@numbers = @subaccount.available_phone_numbers.get('US').local.list({:area_code => '858'})
# buy the first one
@purchased_number = @subaccount.incoming_phone_numbers.create(:phone_number => @numbers[0].phone_number)
phone_number = @purchased_number.phone_number
self[:sms_phone_number] = @purchased_number
end
end
,第二个模型是:
class Employee < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :omniauthable :registerable,
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :timeoutable, :lockable
acts_as_token_authenticatable
validates :first_name, :presence => {:message => 'cannot be blank'}
validates :last_name, :presence => {:message => 'cannot be blank'}
validates :birthday, :presence => {:message => 'cannot be blank'}
# validates :position, :presence => {:message => 'cannot be blank'}
validates :base_rate, :presence => {:message => 'cannot be blank'}
validates :employment_status, :presence => {:message => 'cannot be blank'}
validates_format_of :first_name, :with => /\A[a-zA-Z ]*\z/i,
:message => "can only contain letters and spaces."
validates_format_of :last_name, :with => /\A[a-zA-Z ]*\z/i,
:message => "can only contain letters and spaces."
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :position, :password_confirmation, :current_password, :remember_me, :first_name, :last_name, :tax_file_number,
:phone_number, :address, :state_id, :post_code, :avatar, :location, :employment_type, :hired_at, :birthday
# attr_accessible :title, :body
attr_accessor :current_password
belongs_to :employer
has_many :messages, :dependent => :destroy
has_many :schedules, :dependent => :destroy
has_many :keychains, :dependent => :destroy
has_many :announcements, through: :employer
has_one :position
has_many :employee_locations
has_many :locations, :through => :employee_locations
has_many :tasks
mount_uploader :avatar, AvatarUploader
scope :expect, lambda { joins(:schedules).merge(Schedule.expected)}
def get_full_name
"#{first_name} #{last_name}"
end
def self.search(search)
if search
where("LOWER(first_name) LIKE ? OR LOWER(last_name) LIKE ? OR first_name LIKE ? OR last_name LIKE ?" , "%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%")
else
scoped
end
end
class << self # Class methods
def get_by_employer(employer_id)
self.where('employer_id = ?', employer_id).order('LOWER(last_name) ASC')
end
end
end
错误消息只是声明:
哦不!检测到1个错误,导致该员工无法保存:
未找到电子邮件
只关注员工模式,而不是两者。