# Agile book - getting confusing error

**URL:** <https://www.ruby-forum.com/t/agile-book-getting-confusing-error/68704>\
**Category:** Rails\
**Created:** [February 15, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/agile-book-getting-confusing-error/68704 "2006-02-15T18:32:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 15, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/agile-book-getting-confusing-error/68704/1 "2006-02-15T18:32:03Z")

</div>

Working through the beginning phase from the Agile book on  
‘Administration’

undefined method `hashed_password=' for #<User:0xb7911324> ... /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1498:in`method\_missing’  
#{RAILS\_ROOT}/app/models/user.rb:12:in `before_create' ./script/../config/../app/controllers/login_controller.rb:8:in`add\_user’  
…  
Request  
Parameters: {“user”=\>{“name”=\>“Craig W.”, “login”=\>“craig”,  
“password”=\>“test”}}

OK…

My user.rb includes…

require “digest/sha1”  
class User \< ActiveRecord::Base

attr\_accessor :password  
attr\_accessible :login, :password, :name

def before\_create  
self.hashed\_password = User.hash\_password(self.password)  
end

private

def self.hash\_password(password)  
Digest::SHA1.hexdigest(password)  
end  
end

Where am I going wrong?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 15, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/agile-book-getting-confusing-error/68704/2 "2006-02-15T18:53:16Z")

</div>

Try this in your before\_create() method:

@hashed\_password = User.hash\_password(password)

This will create an instance variable for this User, named  
@hashed\_password and populated with the SHA digested value of the  
password that was entered in the request.

It looks to me like you are using “self” in Ruby as you would use  
“this” in Java, which is going to cause problems like this. They’re  
not equivalent.

Hope this helps,  
David

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 15, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/agile-book-getting-confusing-error/68704/3 "2006-02-15T19:44:51Z")

</div>

That get’s me past the error but then the password column is written as  
null to the db which obviously isn’t the expected behavior. I believe  
the intent is to take the :password and hash it just prior to writing it  
to db so putting the hash into an instance variable would require  
another assignment back…which I will play with but it seems like an  
extra step which I think was the author’s intention of using ‘self’.

I am taking this out of the book so I expected it to work.

Thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 15, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/agile-book-getting-confusing-error/68704/4 "2006-02-15T19:47:55Z")

</div>

On Wed, 2006-02-15 at 18:44 +0100, joey\_\_ wrote:

> > end  
> > Craig
> 
> Try remove the ‘private’, see what happens.

* * *

I tried that before I posted to list…didn’t change anything.

I have tried a lot of stuff - getting stuck following a book is  
frustrating.

Thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 15, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/agile-book-getting-confusing-error/68704/5 "2006-02-15T18:44:39Z")

</div>

Craig W. wrote:

> require “digest/sha1”  
> class User \< ActiveRecord::Base
> 
> attr\_accessor :password  
> attr\_accessible :login, :password, :name
> 
> def before\_create  
> self.hashed\_password = User.hash\_password(self.password)  
> end
> 
> private
> 
> def self.hash\_password(password)  
> Digest::SHA1.hexdigest(password)  
> end  
> end
> 
> Where am I going wrong?
> 
> Craig

Try remove the ‘private’, see what happens.

Joey

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 15, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/agile-book-getting-confusing-error/68704/6 "2006-02-15T20:30:47Z")

</div>

I am so dumb…thanks

column was called ‘password’ and not ‘hashed\_password’

thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 15, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/agile-book-getting-confusing-error/68704/7 "2006-02-15T20:03:15Z")

</div>

Hmm. Does your users table have a column named “hashed\_password”?  
Your model should automatically know about all the columns in its  
corresponding table (User -\> users, in your case).

If your users table doesn’t have this column, you should add it and  
restart your server. If it _does_ have the column, you may still need  
to restart your server (to pick up the most recent version of the  
database).

David
