Skip to content

Commit

Permalink
ActiveRecord::Base#reload should clear dirty attributes. [#231 state:…
Browse files Browse the repository at this point in the history
…resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
ryanb authored and lifo committed May 21, 2008
1 parent 6e3521e commit 262d23d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions activerecord/lib/active_record/dirty.rb
Expand Up @@ -40,6 +40,7 @@ def self.included(base)
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
Expand Down Expand Up @@ -84,6 +85,13 @@ def save_with_dirty!(*args) #:nodoc:
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
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/dirty_test.rb
Expand Up @@ -137,6 +137,14 @@ def test_changed_attributes_should_be_preserved_if_save_failure
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?
Expand Down

0 comments on commit 262d23d

Please sign in to comment.