Jan 17, 2008
Generic-user-small Russell Healy 2 posts

Topic: Agile Web Development with Rails / self vs. @

Ugh, sorry about the formatting. I wish this forum had a preview!

 
Jan 17, 2008
Generic-user-small Russell Healy 2 posts

Topic: Agile Web Development with Rails / self vs. @

I suppose that self.salt = ... is used so that the method generated by rails is called, rather than assigning to the instance variable, which would be making an assumption about the rails implementation. In fact, if you inspect the user (with user.inspect) you will see that rails doesn’t even create an instance variable called @salt. Instead, all of the attributes generated by inspecting the database schema are put in a hash called attributes:

<user:0xb69384cc>“123”, “name”=>”benjamin”, “hashed_password”=>”a335e63a110911315788dbe5b1d44815bdd528ae”, “id”=>2}

The accessor method ‘salt’ must retrieve the value from the @attributes hash.

That doesn’t explain though, the use of self.password and self.salt in the line on page 158:

self.hashed_password = User.encrypted_password(self.password, self.salt)

The following should be sufficient:

self.hashed_password = User.encrypted_password(password, salt)

2 posts