From 80989437dc1502f9194b0600941b6d70a3efa3b2 Mon Sep 17 00:00:00 2001 From: Josh Sharpe Date: Mon, 31 Aug 2009 12:50:27 -0500 Subject: I added this feature so that a Map of changed fields could be retrieved after a model had been saved. This is useful in the after_save callback when you need to know what fields changed. At present there is no way to do this other than have code in the before_save callback that takes a copy of the changes Map, which I thought was a bit messy. Example. person = Person.find_by_name('bob') person.name = 'robert' person.changes # => {'name' => ['bob, 'robert']} person.save person.changes # => {} person.previous_changes # => {'name' => ['bob, 'robert']} person.reload person.previous_changes # => {} Signed-off-by: Joshua Peek --- activerecord/lib/active_record/attribute_methods/dirty.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/attribute_methods') diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index b6c4df2a49..4df0f1af69 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -19,6 +19,7 @@ module ActiveRecord # Attempts to +save+ the record and clears changed attributes if successful. def save_with_dirty(*args) #:nodoc: if status = save_without_dirty(*args) + @previously_changed = changes changed_attributes.clear end status @@ -26,12 +27,18 @@ module ActiveRecord # Attempts to save! the record and clears changed attributes if successful. def save_with_dirty!(*args) #:nodoc: - save_without_dirty!(*args).tap { changed_attributes.clear } + save_without_dirty!(*args).tap do + @previously_changed = changes + changed_attributes.clear + end end # reload the record and clears changed attributes. def reload_with_dirty(*args) #:nodoc: - reload_without_dirty(*args).tap { changed_attributes.clear } + reload_without_dirty(*args).tap do + previously_changed_attributes.clear + changed_attributes.clear + end end private -- cgit v1.2.3