# Self\[:foo\] vs. foo in AR models

**URL:** <https://www.ruby-forum.com/t/self-foo-vs-foo-in-ar-models/125281>\
**Category:** Rails\
**Created:** [January 7, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/self-foo-vs-foo-in-ar-models/125281 "2008-01-07T21:58:11Z")\
**Posts on this page:** 3\
**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:** [January 7, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/self-foo-vs-foo-in-ar-models/125281/1 "2008-01-07T21:58:11Z")

</div>

I have an ActiveRecord model with a field called default\_pic. I can’t  
figure out why the first example DOES NOT work and the second one does,  
I’m  
not sure why I need the self[]

_Doesn’t Work_

def set\_default\_pic  
default\_pic = “d#{(1…8).rand}.gif”  
save  
end

_Works:_

def set\_default\_pic  
_self[:default\_pic]_ = “d#{(1…8).rand}.gif”  
save  
end

---

<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:** [January 7, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/self-foo-vs-foo-in-ar-models/125281/2 "2008-01-07T22:05:15Z")

</div>

On Jan 7, 2008 1:57 PM, blinking bear [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have an ActiveRecord model with a field called default\_pic. I can’t  
> figure out why the first example DOES NOT work and the second one does

It’s a Ruby syntax thing.

> def set\_default\_pic  
> default\_pic = “d#{(1…8).rand}.gif”  
> save  
> end

That sets a local variable named default\_pic. In order to call the  
accessor method, you need to do

self.default\_pic = “d#{(1…8).rand}.gif”

There’s some page in the pickaxe which describes it. I don’t have it  
beside me, sorry.

Pat

---

<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:** [January 7, 2008, 10:41pm UTC](https://www.ruby-forum.com/t/self-foo-vs-foo-in-ar-models/125281/3 "2008-01-07T22:41:45Z")

</div>

On Jan 7, 2008, at 4:04 PM, Pat M. wrote:

> > save  
> > Pat  
> > It’s also an “ActiveRecord handles attributes with method\_missing”  
> > thing.

The idiomatic way of doing the same thing would be to use  
ActiveRecord::Base#update\_attribute.

def set\_default\_pic  
update\_attribute(:default\_pic, “d#{(1…8).rand}.gif”)  
end

Or, if this is part of a before\_create callback, simply:

def set\_default\_pic  
write\_attribute(:default\_pic, “d#{(1…8).rand}.gif”)  
end

assuming, of course, that you have defined Range#rand somewhere to be  
a random item from the Range. (If not, perhaps you want (1+rand(8))  
instead.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
