# Why can't I access attributes in rails model?

**URL:** <https://www.ruby-forum.com/t/why-cant-i-access-attributes-in-rails-model/216246>\
**Category:** Rails\
**Created:** [January 30, 2012, 9:11pm UTC](https://www.ruby-forum.com/t/why-cant-i-access-attributes-in-rails-model/216246 "2012-01-30T21:11:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 30, 2012, 9:11pm UTC](https://www.ruby-forum.com/t/why-cant-i-access-attributes-in-rails-model/216246/1 "2012-01-30T21:11:33Z")

</div>

Hi, I’d like to know why I can’t, or how can I, access attributes like  
that:

class User \< ActiveRecord::Base  
def name  
@first\_name + @last\_name  
end  
end

first\_name and last\_name are user attributes in the database.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![dracko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@dracko](https://www.ruby-forum.com/u/dracko)\
**Post date:** [January 30, 2012, 9:20pm UTC](https://www.ruby-forum.com/t/why-cant-i-access-attributes-in-rails-model/216246/2 "2012-01-30T21:20:52Z")

</div>

Have this instance variables been set anywhere in your app?

If not, you wont be able to access attributes anywhere.

I suggest to first find a record in the DB and set the results to and  
instance variable like:

@var = User.find(params[:some\_params])

and then you will be able to access attributes for that object

@var.name + @var.lastname

2012/1/30 Rodrigo R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dracko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@dracko](https://www.ruby-forum.com/u/dracko)\
**Post date:** [January 30, 2012, 9:27pm UTC](https://www.ruby-forum.com/t/why-cant-i-access-attributes-in-rails-model/216246/3 "2012-01-30T21:27:07Z")

</div>

On Mon, Jan 30, 2012 at 9:09 PM, Rodrigo R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> 

ActiveRecord dynamically defines “accessor methods” on the  
user instance.

That user instance is accessible as ‘self’ inside the user instance,  
so you could do

class User \< ActiveRecord::Base  
def name  
self.first\_name + self.last\_name  
end  
end

But, in Ruby, when using a _read_ accessor, you could also write it  
without the explicit ‘self’.

class User \< ActiveRecord::Base  
def name  
first\_name + last\_name

# same as self.first\_name + self.last\_name

# if there is no first\_name, last\_name local variable

# defined in the scope

end  
end

For _write_ accessors, you _must_ put the self in front, that is:

self.full\_name = “#{first\_name} #{last\_name}”

And the @first\_name you asked about, that is an “instance variable”.

Read one of the many Ruby tutorials or books to understand this better.

HTH,

Peter

---

<div class="post-metadata">

**Author:** ![dracko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@dracko](https://www.ruby-forum.com/u/dracko)\
**Post date:** [January 30, 2012, 10:08pm UTC](https://www.ruby-forum.com/t/why-cant-i-access-attributes-in-rails-model/216246/4 "2012-01-30T22:08:39Z")

</div>

On Mon, Jan 30, 2012 at 9:59 PM, Rodrigo R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Thank you, but I knew that, I did that (use self to do what I want) before  
> I asked the question.
> 
> I’d just like to know why can’t I use @first\_name instead of  
> self.first\_name, to me it seems like the same thing inside the model.

They are not:

@first\_name is an “instance variable”. You could set it like this:

@first\_name = user.first\_name

but it is not automatically set, and it is also not typically used like  
that.

A more typical use would be:

@user = User.find(params[:id])

and then you can use @user in the controller, but also in the views,  
due to the set-up of Rails.

self.first\_name is a method that is dynamically provided by  
ActiveRecord , based on the available columns for the table  
“users” in the database.

HTH,

Peter

---

<div class="post-metadata">

**Author:** ![dracko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@dracko](https://www.ruby-forum.com/u/dracko)\
**Post date:** [January 30, 2012, 10:00pm UTC](https://www.ruby-forum.com/t/why-cant-i-access-attributes-in-rails-model/216246/5 "2012-01-30T22:00:37Z")

</div>

Thank you, but I knew that, I did that (use self to do what I want)  
before  
I asked the question.

I’d just like to know why can’t I use @first\_name instead of  
self.first\_name, to me it seems like the same thing inside the model.

On Mon, Jan 30, 2012 at 6:25 PM, Peter V.

---

<div class="post-metadata">

**Author:** ![dracko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@dracko](https://www.ruby-forum.com/u/dracko)\
**Post date:** [January 31, 2012, 3:30am UTC](https://www.ruby-forum.com/t/why-cant-i-access-attributes-in-rails-model/216246/6 "2012-01-31T03:30:26Z")

</div>

I thought they where attr\_accessor method.

Thank you for clearing things up for me.

On Monday, January 30, 2012, Peter V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Mon, Jan 30, 2012 at 9:59 PM, Rodrigo R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Thank you, but I knew that, I did that (use self to do what I want)  
> > before I asked the question.  
> > I’d just like to know why can’t I use @first\_name instead of  
> > self.first\_name, to me it seems like the same thing inside the model.
> 
> ## They are not: @first\_name is an “instance variable”. You could set it like this: @first\_name = user.first\_name but it is not automatically set, and it is also not typically used like that.
> 
> You received this message because you are subscribed to the Google G.  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> To unsubscribe from this group, send email to  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![dracko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@dracko](https://www.ruby-forum.com/u/dracko)\
**Post date:** [January 31, 2012, 12:33pm UTC](https://www.ruby-forum.com/t/why-cant-i-access-attributes-in-rails-model/216246/7 "2012-01-31T12:33:43Z")

</div>

On Jan 31, 2:29am, Rodrigo R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I thought they where attr\_accessor method.
> 
> Thank you for clearing things up for me.

To add a little more info, the attribute values are stored in the  
@attributes / @attributes\_cache hashes

Fred
