Rails 3: alias_attribute and Unkown column error

I use Devise for authentication, so I used the aliases of several columns in my old database to place them 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 submit a message 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 believe that I was on the assumption that alias_attributeActiveRecord would be instructed to use the name of the real column (UserId) and not the alias (username). What am I doing wrong?

+3
source share
1 answer

Arel (, SQL-) - ActiveRecord ( 3.0.3). , LoginId.

User.where(: username = > "root" ), , , User.username .

sinup, .

EDIT: , view! ! http://www.slideshare.net/napcs/rails-and-legacy-databases-railsconf-2009

+6

Source: https://habr.com/ru/post/1780016/


All Articles