# @ vs self in models?

**URL:** <https://www.ruby-forum.com/t/vs-self-in-models/91268>\
**Category:** Rails\
**Created:** [April 6, 2007, 4:30am UTC](https://www.ruby-forum.com/t/vs-self-in-models/91268 "2007-04-06T04:30:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [April 6, 2007, 4:30am UTC](https://www.ruby-forum.com/t/vs-self-in-models/91268/1 "2007-04-06T04:30:16Z")

</div>

i’ve seen both of these syntaxes before and have also used both in my  
own models:

class Foo  
def get\_bar  
@bar  
end  
end

and

class Foo  
def get\_bar  
self.bar  
end  
end

seemingly with no difference… i’m a nuby though so could anyone  
enlighten me to the differences if there are any?

thanks,  
jeff

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [April 6, 2007, 5:05am UTC](https://www.ruby-forum.com/t/vs-self-in-models/91268/2 "2007-04-06T05:05:17Z")

</div>

@bar is an instance variable… self.bar would be a method. Try doing  
this code in irb and you’ll see that calling get\_bar in the first  
version returns nil, while calling it on the second version throws a  
NoMethodError.

b

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [April 6, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/vs-self-in-models/91268/3 "2007-04-06T17:58:33Z")

</div>

aha! i guess then the reason it has worked in RoR models is due to AR  
creating attr\_accessors for the table columns, so calling  
model.some\_col was returning @some\_col… thanks for clearing that up

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [April 6, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/vs-self-in-models/91268/4 "2007-04-06T20:00:26Z")

</div>

Jeff Emminger wrote:

> i’ve seen both of these syntaxes before and have also used both in my  
> own models:
> 
> class Foo  
> def get\_bar  
> @bar  
> end  
> end
> 
> and
> 
> class Foo  
> def get\_bar  
> self.bar  
> end  
> end
> 
> seemingly with no difference… i’m a nuby though so could anyone  
> enlighten me to the differences if there are any?
> 
> thanks,  
> jeff

This can be a real Gotcha in ActiveRecord. AR uses the _missing  
methods to give the illusion that it knows what columns you have in your  
table. I believe that using the instance variable (@bar) now spews lots  
of depreciation noeses onto stdout. Good programming practice it to  
used the method call because you can then override that method call in  
your class and massage the data before it goes out (or comes in). This  
can eliminate the need for the get_ functions.

def bar  
retval = self[:bar]  
#do interesting thing with retval here  
retval  
end

John M.
