2

I am running into some slightly tricky issues with a legacy db. Everything seems to work fine if I simply change the "password" column name in the db to "encrypted_password"; however, I need to leave the db in place.

So I decided to use

alias_attribute :encrypted_password, :password

Now I get a "stack level too deep" error in the console.

My user model:

class User < ActiveRecord::Base
  require "digest/sha1"
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :encryptable, :encryptor => :old_cakephp_auth

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  has_many :events

  before_create :add_default_values

  #alias_attribute :created_at, :created  
  #alias_attribute :updated_at, :updated
  alias_attribute :encrypted_password, :password

  def add_default_values
    self.created = Time.now
    self.updated = Time.now
    self.image = ""
    self.name = self.email.split("@").first
    #make normal user
    self.role_id = 2
    self.username = self.email.split("@").first + rand(100000000000000).to_s
    self.website = ""
  end

  def valid_password?(password)
    return false if encrypted_password.blank?
    Devise.secure_compare(Digest::SHA1.hexdigest(self.password_salt+password), self.encrypted_password)
  end
end

Ideas? Thanks!!! :)

Parris
  • 17,833
  • 17
  • 90
  • 133

1 Answers1

2

I imagine that this is due to devise reserving the word password for their own use (and it in turn calling encrypted_password. Try renaming it to pword and see if the error still occurs. If it doesn't, you'll have to find another name to call the aliased password.

I should say that this is just an assumption. Let me know if it helps.

Chuck Callebs
  • 16,293
  • 8
  • 56
  • 71
  • So, that gives me "NoMethodError (undefined method `pword=...)", which makes sense... got any ideas for a work around? Is there any way to create an alias for a column name in mysql? – Parris Mar 27 '12 at 01:25
  • The only way I know to alias is the way that you've been doing it (See this question for further information: http://stackoverflow.com/questions/4014831/alias-for-column-names-in-rails). Did you add `pword` to the attr_accessible? If you did, and are still getting the error, you might try implementing the alias by brute force (creating the get/set methods manually). – Chuck Callebs Mar 27 '12 at 01:38
  • Thanks! my interim solution until we can change the db is to simply copy the db field and name it encrypted_password. This seems to work. Eventually password would get phased out. So i think we are fine. – Parris Mar 27 '12 at 02:01