# ActiveRecord: why does 'self.name' and 'name' both work?

**URL:** <https://www.ruby-forum.com/t/activerecord-why-does-self-name-and-name-both-work/108283>\
**Category:** Rails\
**Created:** [July 1, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/activerecord-why-does-self-name-and-name-both-work/108283 "2007-07-01T18:11:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [July 1, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/activerecord-why-does-self-name-and-name-both-work/108283/1 "2007-07-01T18:11:44Z")

</div>

I’m trying to figure out why both ‘self.name’ and ‘name’ work in a  
model class. For example:

class Category \< ActiveRecord::Base  
before\_save :set\_permalink  
private  
def set\_permalink  
# both work  
self.permalink = name  
self.permalink = self.name  
# doesn’t work  
permalink = name  
@permalink = name  
@permalink = @name  
self.permalink = @name  
end  
end

I thought that attributes in a class were set via instance methods,  
but ‘self.permalink = @name’ doesn’t work, but ‘name’ (which looks  
like a local variable to me) works fine. And self.permalink works, but  
@permalink doesn’t.

I’m still new to Ruby and classes so it’s probably something simple I  
don’t understand, so any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [July 1, 2007, 6:24pm UTC](https://www.ruby-forum.com/t/activerecord-why-does-self-name-and-name-both-work/108283/2 "2007-07-01T18:24:45Z")

</div>

Basically name and self.name are interpreted by ruby as methods…

because your model when mapped to its corresponding table has a  
column called ‘name’, rails creates the relevant method instance.name.

Therefore your reference to name and self.name are calling the same  
method… as name (without the self.) is scoped to the instance method  
(as its inside an instance method itself), and self evaluates to the  
instance, therefore it calls the instance method ‘name’…

# both work

```
 self.permalink = name <-- calls method name, and its value is

```

passed to the method ‘permalink’ (again, not an variable really)  
self.permalink = self.name \<-- calls same method… as self.  
evaluates to the instance  
# doesn’t work  
permalink = name \<-- ruby is probably getting confused here…  
which is probably why its not working…  
@permalink = name \<-- again this is probably being set … but it  
wont update your model as it is saved… because @permalink is a var  
self.permalink is the method you need to call to update your model…  
@permalink = @name \<— again they are not instance vars. … so  
@permalink =\> nil but you wont see it in your db…  
self.permalink = @name \<-- @name doesnt exist … so will be nil…

I hope that makes sense… sunday afternoon in the office has left my  
brain a bit fried… (mainly because im writing actionscript…)

Good luck!

Jason.

On 1 Jul 2007, at 17:10, rob wrote:

I’m trying to figure out why both ‘self.name’ and ‘name’ work in a  
model class. For example:

class Category \< ActiveRecord::Base  
before\_save :set\_permalink  
private  
def set\_permalink  
# both work  
self.permalink = name  
self.permalink = self.name  
# doesn’t work  
permalink = name  
@permalink = name  
@permalink = @name  
self.permalink = @name  
end  
end

I thought that attributes in a class were set via instance methods,  
but ‘self.permalink = @name’ doesn’t work, but ‘name’ (which looks  
like a local variable to me) works fine. And self.permalink works, but  
@permalink doesn’t.

I’m still new to Ruby and classes so it’s probably something simple I  
don’t understand, so any help would be appreciated.
