I am using Devise for authentication, so I've aliased a few columns in my legacy database to accommodate it as follows:
class User < ActiveRecord::Base set_table_name 'my_legacy_user_table' set_primary_key 'UserId' alias_attribute :id, :UserId alias_attribute :username, :LoginId alias_attribute :encrypted_password, :PasswordSHA1Hash alias_attribute :first_name, :Name alias_attribute :last_name, :Surname devise :database_authenticatable, :authentication_keys => [:username] attr_accessible :username, :password, :password_confirmation def password_salt=(password_salt) end def password_salt end def password_digest(password) self.class.encryptor_class.digest(password) end end
When I post to my /users/sign_in form, I get the following exception:
Mysql2::Error: Unknown column 'my_legacy_user_table.username' in 'where clause': SELECT `kms_User`.* FROM `my_legacy_user_table` WHERE (`my_legacy_user_table`.`username` = 'mrichman') LIMIT 1
I suppose I was under the assumption that alias_attribute
would instruct ActiveRecord to use the real column name (UserId) and not the alias (username). What am I doing wrong?