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! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On May 25, 4:33 pm, Kendall Gifford <zettab...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.