views:

121

answers:

1

I have a User model that acts_as_authentic for AuthLogic's password management. AuthLogic adds "password" and "password_confirmation" attributes over top of the db-backed "crypted_password" attribute. This is pretty standard AuthLogic stuff.

I want to have a method that sets both password and password_confirmation at the same time (useful for internal applications where I'm not worried about typos). To do this I created a new method in User:

#user.rb
def password_and_confirm=(value)
  password = value
  password_confirmation = value
end

However calling this method does not seem to actually set the password:

user = User.new
user.password = "test"
user.password               # => "test"
user.crypted_password       # => a big base64 string, as expected

user = User.new
user.password_and_confirm = "test"
user.password               # => nil
user.crypted_password       # => nil

I also tried a different route:

def internal_password(value)
  password = value
end

...and got the same problem.

Why can't I set the password attribute from within a method inside the User class?

+1  A: 

Better try this:

#user.rb
def password_and_confirm=(value)
  self.password = value
  self.password_confirmation = value
end

Otherwise ruby tries to treat the methods (as it is implemented as such) as local variables (this has precedence during assignment operations).

hurikhan77
That did it... thanks!
Craig Walker