# Setting model attributes

**URL:** <https://www.ruby-forum.com/t/setting-model-attributes/134045>\
**Category:** Rails\
**Created:** [April 7, 2008, 10:56am UTC](https://www.ruby-forum.com/t/setting-model-attributes/134045 "2008-04-07T10:56:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [April 7, 2008, 10:56am UTC](https://www.ruby-forum.com/t/setting-model-attributes/134045/1 "2008-04-07T10:56:39Z")

</div>

Hi,

I’d like to clear up a hole in my understanding.

Why does the following not work in a model:

def update\_a\_model\_attribute(new\_value)  
@model\_attribute = new\_value  
save  
end

But the following does:  
def update\_a\_model\_attribute(new\_value)  
self.model\_attribute = new\_value  
save  
end

When I say it does not work, I mean that the attribute does not get  
updated in the database.

Many thanks,  
John

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [April 7, 2008, 11:54am UTC](https://www.ruby-forum.com/t/setting-model-attributes/134045/2 "2008-04-07T11:54:53Z")

</div>

John L. wrote:

> def update\_a\_model\_attribute(new\_value)  
> @model\_attribute = new\_value  
> save  
> end

This is setting an instance variable.

> But the following does:  
> def update\_a\_model\_attribute(new\_value)  
> self.model\_attribute = new\_value  
> save  
> end

This is setting a model attribute.

In the console, run:

SomeModel.find(:first).instance\_variables

You’ll see that the attributes are not implemented as instance  
variables…

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [April 7, 2008, 1:27pm UTC](https://www.ruby-forum.com/t/setting-model-attributes/134045/3 "2008-04-07T13:27:48Z")

</div>

Thanks, that’s interesting. I wonder why they were not implemented as  
instance variables. It would appear to me to be a logical choice.
