aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyan Bates <ryan@railscasts.com>2008-05-21 08:27:20 -0700
committerPratik Naik <pratiknaik@gmail.com>2008-05-21 20:40:52 +0100
commit262d23d763c05bbe5f433a99cb9f02e48a8cdd4a (patch)
tree3e4c227cabb899746db0d6f23beb15ac7b10e520 /activerecord
parent6e3521e6134e7f0d0b27834266e41354290c4e7e (diff)
downloadrails-262d23d763c05bbe5f433a99cb9f02e48a8cdd4a.tar.gz
rails-262d23d763c05bbe5f433a99cb9f02e48a8cdd4a.tar.bz2
rails-262d23d763c05bbe5f433a99cb9f02e48a8cdd4a.zip
ActiveRecord::Base#reload should clear dirty attributes. [#231 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/dirty.rb8
-rw-r--r--activerecord/test/cases/dirty_test.rb8
2 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/dirty.rb b/activerecord/lib/active_record/dirty.rb
index 49f1204fa1..8fdc763292 100644
--- a/activerecord/lib/active_record/dirty.rb
+++ b/activerecord/lib/active_record/dirty.rb
@@ -40,6 +40,7 @@ module ActiveRecord
base.alias_method_chain :save, :dirty
base.alias_method_chain :save!, :dirty
base.alias_method_chain :update, :dirty
+ base.alias_method_chain :reload, :dirty
base.superclass_delegating_accessor :partial_updates
base.partial_updates = false
@@ -84,6 +85,13 @@ module ActiveRecord
status
end
+ # <tt>reload</tt> the record and clears changed attributes.
+ def reload_with_dirty(*args) #:nodoc:
+ record = reload_without_dirty(*args)
+ changed_attributes.clear
+ record
+ end
+
private
# Map of change attr => original value.
def changed_attributes
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index 62178977f9..c011ffaf57 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -137,6 +137,14 @@ class DirtyTest < ActiveRecord::TestCase
check_pirate_after_save_failure(pirate)
end
+ def test_reload_should_clear_changed_attributes
+ pirate = Pirate.create!(:catchphrase => "shiver me timbers")
+ pirate.catchphrase = "*hic*"
+ assert pirate.changed?
+ pirate.reload
+ assert !pirate.changed?
+ end
+
private
def with_partial_updates(klass, on = true)
old = klass.partial_updates?