# Dirty objects

**URL:** <https://www.ruby-forum.com/t/dirty-objects/177860>\
**Category:** Rails\
**Created:** [October 25, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/dirty-objects/177860 "2009-10-25T18:25:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [October 25, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/dirty-objects/177860/1 "2009-10-25T18:25:37Z")

</div>

Hello all…I understand how dirty objects work and have gone into  
script/console and successfully tried it. What I don’t understand is how  
I can use it in an application. All references to its use are via  
console.  
I want to send an email when someone changes a field in the record. I  
would think I could do this with the ‘update’ method in the controller:

def update  
@log = Log.find(params[:id])  
if @log.update\_attributes(params[:log])  
#flash[:notice] = “Successfully updated log.”  
redirect\_to @log  
else  
render :action =\> ‘edit’  
end  
end

Doing something like @log.changes doesn’t work as @log was just  
retrieved from the database and has no changes. params[:log] has all the  
new values from the edit form but you can’t do params[:log].changes.  
Hope this is making sense, any help appreciated…Bill

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [October 25, 2009, 6:42pm UTC](https://www.ruby-forum.com/t/dirty-objects/177860/2 "2009-10-25T18:42:14Z")

</div>

Bill McG wrote:  
[…]

> I want to send an email when someone changes a field in the record. I  
> would think I could do this with the ‘update’ method in the controller:

[…]

> Doing something like @log.changes doesn’t work as @log was just  
> retrieved from the database and has no changes.

So don’t do it in the controller. Use a before\_update callback instead.  
That should give you access to the model object without having to  
retrieve it from the DB again.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [October 25, 2009, 7:45pm UTC](https://www.ruby-forum.com/t/dirty-objects/177860/3 "2009-10-25T19:45:52Z")

</div>

Marnen Laibow-Koser wrote:

> Bill McG wrote:  
> […]
> 
> > I want to send an email when someone changes a field in the record. I  
> > would think I could do this with the ‘update’ method in the controller:
> 
> […]
> 
> > Doing something like @log.changes doesn’t work as @log was just  
> > retrieved from the database and has no changes.
> 
> So don’t do it in the controller. Use a before\_update callback instead.  
> That should give you access to the model object without having to  
> retrieve it from the DB again.
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

Work well - thanks

class Log \< ActiveRecord::Base  
belongs\_to :runner

def before\_save  
UserMailer.deliver\_notification(self.changes)  
end  
end
