# Attr\_accessor :password

**URL:** <https://www.ruby-forum.com/t/attr-accessor-password/177269>\
**Category:** Rails\
**Created:** [October 14, 2009, 11:34am UTC](https://www.ruby-forum.com/t/attr-accessor-password/177269 "2009-10-14T11:34:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@ralu](https://www.ruby-forum.com/u/ralu)\
**Post date:** [October 14, 2009, 11:34am UTC](https://www.ruby-forum.com/t/attr-accessor-password/177269/1 "2009-10-14T11:34:21Z")

</div>

attr\_accessor :password  
dis is small bit of code i have seen in a tutorial .

in that example there is no field called password in the database, so  
how can rails understand its the password field of my database. in my  
database i have given password field as hashed\_password

i got an explanation form the tutorial as given below  
" The attr\_accessor statement specifies the attribute password. You  
may have noticed that  
this doesnâ€™t actually exist as a field in the database migration. This  
declares that password is an  
attribute of the model even though it doesnâ€™t exist as a database field.  
The model will be able to  
use the data from this field to create the hashed\_password field. You  
will notice that this is done  
by the before\_save filter. This will simply set the hashed\_password  
field to be a hash of the clear  
password which, as stated in our specification, we can compare to the  
stored hashed\_password  
value to authenticate a userâ€™s login credentials.  
"

but i ddn understand clearly… can any explain in better way!

---

<div class="post-metadata">

**Author:** ![ralu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@ralu](https://www.ruby-forum.com/u/ralu)\
**Post date:** [October 14, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/attr-accessor-password/177269/2 "2009-10-14T12:10:32Z")

</div>

On Wed, Oct 14, 2009 at 2:34 AM, Ralu rm  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> may have noticed that  
> value to authenticate a userâ€™s login credentials.  
> "
> 
> but i ddn understand clearly… can any explain in better way!

Where can one find this example?

-Conrad

---

<div class="post-metadata">

**Author:** ![ralu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@ralu](https://www.ruby-forum.com/u/ralu)\
**Post date:** [October 14, 2009, 12:21pm UTC](https://www.ruby-forum.com/t/attr-accessor-password/177269/3 "2009-10-14T12:21:33Z")

</div>

Conrad T. wrote:

> On Wed, Oct 14, 2009 at 2:34 AM, Ralu rm  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > may have noticed that  
> > value to authenticate a userâ€™s login credentials.  
> > "
> > 
> > but i ddn understand clearly… can any explain in better way!
> 
> Where can one find this example?
> 
> -Conrad

i got dat from tutorial . for me the expalnation was not dat gud  
so can any one help me with dat

---

<div class="post-metadata">

**Author:** ![ralu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@ralu](https://www.ruby-forum.com/u/ralu)\
**Post date:** [October 14, 2009, 12:43pm UTC](https://www.ruby-forum.com/t/attr-accessor-password/177269/4 "2009-10-14T12:43:54Z")

</div>

On Wed, Oct 14, 2009 at 3:21 AM, Ralu rm  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > > 
> > 
> > Where can one find this example?
> > 
> > -Conrad
> 
> i got dat from tutorial . for me the expalnation was not dat gud  
> so can any one help me with dat

Where’s the complete class?

---

<div class="post-metadata">

**Author:** ![ralu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@ralu](https://www.ruby-forum.com/u/ralu)\
**Post date:** [October 14, 2009, 1:19pm UTC](https://www.ruby-forum.com/t/attr-accessor-password/177269/5 "2009-10-14T13:19:40Z")

</div>

Conrad T. wrote:

> On Wed, Oct 14, 2009 at 3:21 AM, Ralu rm  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > > > 
> > > 
> > > Where can one find this example?
> > > 
> > > -Conrad
> > 
> > i got dat from tutorial . for me the expalnation was not dat gud  
> > so can any one help me with dat
> 
> Where’s the complete class?

require ‘digest/sha2’  
class User \< ActiveRecord::Base  
attr\_protected :hashed\_password, :enabled  
attr\_accessor :password  
validates\_presence\_of :username  
validates\_presence\_of :email  
validates\_presence\_of :password, :if =\> :password\_required?  
validates\_presence\_of :password\_confirmation, :if =\>  
:password\_required?  
validates\_confirmation\_of :password, :if =\> :password\_required?  
validates\_uniqueness\_of :username, :case\_sensitive =\> false  
validates\_uniqueness\_of :email, :case\_sensitive =\> false  
validates\_length\_of :username, :within =\> 3…64  
validates\_length\_of :email, :within =\> 5…128  
validates\_length\_of :password, :within =\> 4…20, :if =\>  
:password\_required?  
validates\_length\_of :profile, :maximum =\> 1000  
def before\_save  
self.hashed\_password = User.encrypt(password) if  
!self.password.blank?  
end  
def password\_required?  
self.hashed\_password.blank? || !self.password.blank?  
end  
def self.encrypt(string)  
return Digest::SHA256.hexdigest(string)  
end  
end

---

<div class="post-metadata">

**Author:** ![ralu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@ralu](https://www.ruby-forum.com/u/ralu)\
**Post date:** [October 14, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/attr-accessor-password/177269/6 "2009-10-14T14:15:15Z")

</div>

On Wed, Oct 14, 2009 at 4:19 AM, Ralu rm  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > >
> 
> validates\_presence\_of :username  
> :password\_required?  
> end  
> end

‘attr\_accessor :password’ will generate two instance methods for class  
User  
similar  
to the following:

def password=(val)  
@password = val  
end

def password  
@password  
end

From the above, the instance variable, password, is brought into  
scope within the User class when ‘@password = val’ is executed. In  
Ruby, you can add an instance variable to a class by simply assigning  
a value to instance variable within an instance method. Then executing  
it. Finally, I would recommend learning more about Ruby by reading  
“Programming Ruby” or “Programming Ruby 1.9”.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![ralu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@ralu](https://www.ruby-forum.com/u/ralu)\
**Post date:** [October 15, 2009, 6:25am UTC](https://www.ruby-forum.com/t/attr-accessor-password/177269/7 "2009-10-15T06:25:43Z")

</div>

Conrad T. wrote:

> On Wed, Oct 14, 2009 at 4:19 AM, Ralu rm  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > > >
> > 
> > validates\_presence\_of :username  
> > :password\_required?  
> > end  
> > end
> 
> ‘attr\_accessor :password’ will generate two instance methods for class  
> User  
> similar  
> to the following:
> 
> def password=(val)

thanks …ill go for that
