# Calling save on model object does not generate a sql update

**URL:** <https://www.ruby-forum.com/t/calling-save-on-model-object-does-not-generate-a-sql-update/154524>\
**Category:** Rails\
**Created:** [December 1, 2008, 9:36am UTC](https://www.ruby-forum.com/t/calling-save-on-model-object-does-not-generate-a-sql-update/154524 "2008-12-01T09:36:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wbwbwb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/aeb1de/32.png) [@wbwbwb](https://www.ruby-forum.com/u/wbwbwb)\
**Post date:** [December 1, 2008, 9:36am UTC](https://www.ruby-forum.com/t/calling-save-on-model-object-does-not-generate-a-sql-update/154524/1 "2008-12-01T09:36:24Z")

</div>

I have been using RoRfor a few days, so excuse me if this is a basic  
question.

I am trying to save a model object from an action in a controller.

When I call a method on the model object that causes a state change,  
then call save, no sql is generated.  
However, if I modify the model object property directly from the  
controller, then call save, sql is generated.

Here is an example of what I am talking about:

## Model object

class MyClass \< ActiveRecord::Base  
def modifyModel  
aDate = Date.today  
end  
end

## Controller

# this method generates a sql update

def method1  
@myClass = MyClass.find(params[:id])  
@myClass.aDate = Date.today  
@budget\_entry.save!

```
<...rest of code omitted...>

```

end

# this method does not generate a sql update

def method2  
@myClass = MyClass.find(params[:id])  
@myClass.modifyModel  
@budget\_entry.save!

```
<...rest of code omitted...>

```

end

Can someone please steer me in the right direction? I want to  
encapsulate the state change to certain methods of the model.

---

<div class="post-metadata">

**Author:** ![wbwbwb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/aeb1de/32.png) [@wbwbwb](https://www.ruby-forum.com/u/wbwbwb)\
**Post date:** [December 1, 2008, 9:38am UTC](https://www.ruby-forum.com/t/calling-save-on-model-object-does-not-generate-a-sql-update/154524/2 "2008-12-01T09:38:46Z")

</div>

On Dec 1, 12:51 am, wb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have been using RoRfor a few days, so excuse me if this is a basic  
> question.
> 
> I am trying to save a model object from an action in a controller.
> 
> When I call a method on the model object that causes a state change,  
> then call save, no sql is generated.  
> However, if I modify the model object property directly from the  
> controller, then call save, sql is generated.

The first thing is that save on an unchanged object is a no-op as of  
rails 2.1 (when partial updates was added).

> ```
> def modifyModel
> aDate = Date.today
> end
> 
> ```

This method does not change the aDate attribute: it just sets a local  
variable called aDate (which is forgotten about as soon as that method  
returns). To disambiguate and force ruby to call your accessor you  
need to do self.aDate = Date::today

Fred
