Database trigger and callback

If I wanted to write my own db trigger using a 'before_save" callback,
how would I get the before save and after save values?

Thanks!

On Wednesday, May 25, 2011 9:06:49 AM UTC-6, Mlle wrote:

If I wanted to write my own db trigger using a 'before_save" callback,
how would I get the before save and after save values?

Thanks!

By “before save and after save values” do you mean the original,
unchanged
value(s) of the propert(y|ies) and the new, changed value(s)?

If so, you might be interested in #changed_attributes. If you call this
on a
model instance, it returns a hash where the keys are the names of the
model
attributes that have changed and the associated values are original
values.
For example:

x = User.find(1)
=> #<User id: 1, name: “Bill”, …>
x.changed?
=> false
x.name
=> “Bill”
x.name = “Fred”
=> “Fred”
x.changed?
=> true
x.changed_attributes
=> { “name” => “Bill” }
x.save
=> true
x.changed?
=> false
x.changed_attributes
=> {}

For more information:
http://api.rubyonrails.org/classes/ActiveModel/Dirty.html

I’m now following this method - using a callback to log the changed
attributes in my audit table for the model.

The only thing is, I’d like to grab the name of the logged-in user and
save it in the audit table. Is there any way of doing that? I can
get it in the controller, but not in the model by using
current_admin_user.login

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