# Reading n writing object attributes

**URL:** <https://www.ruby-forum.com/t/reading-n-writing-object-attributes/151930>\
**Category:** Rails\
**Created:** [October 28, 2008, 2:49am UTC](https://www.ruby-forum.com/t/reading-n-writing-object-attributes/151930 "2008-10-28T02:49:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [October 28, 2008, 2:49am UTC](https://www.ruby-forum.com/t/reading-n-writing-object-attributes/151930/1 "2008-10-28T02:49:55Z")

</div>

Reading n writing object attributes

Hi. In this simple example I have a User class with the class method:

def self.find\_user(name)  
user = self.find(:first, :conditions =\> [“username = ?”, name])  
# user.salt  
end

In the rails console I do the following:

> > u = User.find\_user(‘kena’)  
> > =\> #\<User id: 5, username: “test”, hashed\_password:  
> > “c1a18d933c06f5a3ebd8d1d155d294f39447fbf2”, salt:  
> > “177647700.129851651671097”, email: “”, created\_at: “2008-10-28  
> > 00:54:42”, updated\_at: “2008-10-28 00:54:42”\>
> 
> > u.salt  
> > =\> nil
> 
> > u.username  
> > =\> nil

What I don’t understand is why I can’t access its attributes?

---

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [October 28, 2008, 4:11am UTC](https://www.ruby-forum.com/t/reading-n-writing-object-attributes/151930/2 "2008-10-28T04:11:34Z")

</div>

Soh,

Not sure why you are not able to access the attributes in the use case  
but can I ask why you are not using the dynamic find\_by methods ie  
User.find\_by\_username(‘kena’) ?

–  
Robert Z.  
Zapient, LLC  
Ruby on Rails Development and Consulting

On Oct 27, 6:49 pm, Soh D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [October 28, 2008, 1:36pm UTC](https://www.ruby-forum.com/t/reading-n-writing-object-attributes/151930/3 "2008-10-28T13:36:45Z")

</div>

Robert Z. wrote:

> Soh,
> 
> Not sure why you are not able to access the attributes in the use case  
> but can I ask why you are not using the dynamic find\_by methods ie  
> User.find\_by\_username(‘kena’) ?
> 
> –  
> Robert Z.  
> Zapient, LLC  
> Ruby on Rails Development and Consulting
> 
> On Oct 27, 6:49ï¿½pm, Soh D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I will definitely use the dynamic method, but I was just wondering why  
the other way was going wrong. I didn’t have any private nor protected  
methods and I was reloading my console with: reload! … I then decided  
to re-start my machine and now it works fine. Unfortunately I cannot  
reproduce that problem now … let’s consider that a good thing …  
thanks anyway 🙂
