Dirty objects

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

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
[email protected]

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
[email protected]

Work well - thanks

class Log < ActiveRecord::Base
belongs_to :runner

def before_save
UserMailer.deliver_notification(self.changes)
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs