Setting model attributes

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

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…

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