# Newbie question - self.var OR @var in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/newbie-question-self-var-or-var-in-activerecord/104617>\
**Category:** Rails\
**Created:** [May 20, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/newbie-question-self-var-or-var-in-activerecord/104617 "2007-05-20T20:08:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![itsnotvalid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e8c25b/32.png) [@itsnotvalid](https://www.ruby-forum.com/u/itsnotvalid)\
**Post date:** [May 20, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/newbie-question-self-var-or-var-in-activerecord/104617/1 "2007-05-20T20:08:00Z")

</div>

I started learning rails by following the agile way. It comes to the  
point where it puzzles me.

class Puzzle \< ActiveRecord::Base

# virtual var, not existing in the database

def var  
@var  
end

def var=(foo)  
@var= foo  
@var\_in\_database = @var + 1 # var\_in\_database exists in database  
end

end

Then when I do a save of this model it does nothing. I know that if I  
instead put self.var\_in\_database it would work perfectly, but from my  
limited knowledge of ruby @var should be working like self.var in most  
cases. Now it is the rare case but I can’t find the reason why this  
doesn’t work.

Please solve my puzzle, thank you.

---

<div class="post-metadata">

**Author:** ![itsnotvalid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e8c25b/32.png) [@itsnotvalid](https://www.ruby-forum.com/u/itsnotvalid)\
**Post date:** [May 20, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/newbie-question-self-var-or-var-in-activerecord/104617/2 "2007-05-20T21:20:12Z")

</div>

The answer is easy - @var and self.var are not the same. All  
variables in Ruby (it’s a Ruby question, not Rails) are private and  
you can never make them public directly. What is usually done is  
creating getters and seters with the same name. Like this

class Foo  
def var  
@var  
end

def var=(v)  
@var = v  
end  
end

or like this (which does he same for you):

class Foo  
attr\_acessor :var  
end

So when you are talking about ActiveRecord objects, you must use  
self.var, to call the magic methods created for you by ActiveRecord.

Cheers,  
Yuri

On 5/20/07, itsnotvalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> cases. Now it is the rare case but I can’t find the reason why this  
> doesn’t work.
> 
> Please solve my puzzle, thank you.
> 
> >

–  
Best regards,  
Yuri L.

---

<div class="post-metadata">

**Author:** ![itsnotvalid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e8c25b/32.png) [@itsnotvalid](https://www.ruby-forum.com/u/itsnotvalid)\
**Post date:** [May 20, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/newbie-question-self-var-or-var-in-activerecord/104617/3 "2007-05-20T21:22:12Z")

</div>

> All variables in Ruby (it’s a Ruby question, not Rails) are private  
> I meant all object variables here
