# When to use self\[:foo\] in models

**URL:** <https://www.ruby-forum.com/t/when-to-use-self-foo-in-models/104223>\
**Category:** Rails\
**Created:** [May 15, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/when-to-use-self-foo-in-models/104223 "2007-05-15T17:28:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [May 15, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/when-to-use-self-foo-in-models/104223/1 "2007-05-15T17:28:23Z")

</div>

I’ve always been a little unclear on accessing attributes of an  
ActiveRecord  
model within the class itself, i.e.

def full\_name  
“#{first\_name} #{last\_name}”  
end

or

def full\_name  
“#{self[:first\_name]} #{self[:last\_name]}”  
end

This is a contrieved example and I think both work, but I’ve found other  
times where doing something similar doesn’t work without the self[:].  
Can  
someone shed some light on what the difference is?

Thanks!

---

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [May 15, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/when-to-use-self-foo-in-models/104223/2 "2007-05-15T20:00:32Z")

</div>

> This is a contrieved example and I think both work, but I’ve found other  
> times where doing something similar doesn’t work without the self[:]. Can  
> someone shed some light on what the difference is?

I think it’s preferable to use the methods rather than the hash  
interface. The only situation I’ve come across where you cannot use  
the method is single-table inheritance, where you have to use  
self[:type] instead of type because type is a built-in (though  
deprecated) method.

- Mark.
