# In the callback why I must use self

**URL:** <https://www.ruby-forum.com/t/in-the-callback-why-i-must-use-self/139059>\
**Category:** Ruby\
**Created:** [May 30, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/in-the-callback-why-i-must-use-self/139059 "2008-05-30T15:18:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 30, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/in-the-callback-why-i-must-use-self/139059/1 "2008-05-30T15:18:39Z")

</div>

class User \< ActiveRecord::Base  
def before\_save  
self.name1 = fname + ’ ’ + lname  
name2 = fname + ’ ’ + lname  
end  
end

In the callback I am using self for name1 and NOT using self for name2.  
When a record is saved in the database I get proper value for name1 but  
not for name2. Question is why?

In my judgement before name2 whether I use self or not should not matter  
because there is an implicit self when there is no receiver. Then why  
the difference in behavior?

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 30, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/in-the-callback-why-i-must-use-self/139059/2 "2008-05-30T16:15:31Z")

</div>

On 5/30/08, Raj S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When a record is saved in the database I get proper value for name1 but

The issue here is that the parser can’t tell the difference between  
calling the method #name2= or creating a new local variable named  
“name2”

In this case, the proper course of action is to use "self.name2 = " to  
make sure there is no confusion.

Jason

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 30, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/in-the-callback-why-i-must-use-self/139059/3 "2008-05-30T16:22:16Z")

</div>

On May 30, 2008, at 9:32 AM, Jason R. wrote:

> The issue here is that the parser can’t tell the difference between  
> calling the method #name2= or creating a new local variable named  
> “name2”
> 
> In this case, the proper course of action is to use "self.name2 = " to  
> make sure there is no confusion.
> 
> Jason

…and the reason Ruby can’t has to do with the dynamic way that  
ActiveRecord provides model attributes. Ruby can’t introspect on the  
object to find that name2= exists so it is quite happy with calling  
name2 a local variable. If you used name2 on the first line  
(self.name1 = fname + name2) then Ruby would have to assume that name2  
is a method since it would otherwise be an undefined local variable.  
(I don’t know if it would then find name2= for the next line, but I  
suspect not. That would be easy for you to try anyway.)

-Rob

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