Skip to content

Commit

Permalink
Exclude unchanged records from the collection being considered for au…
Browse files Browse the repository at this point in the history
…tosave. [#2578 state:resolved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information
lawrencepit authored and alloy committed Jan 8, 2010
1 parent 1080351 commit 5193fe9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -224,10 +224,10 @@ def marked_for_destruction?
def associated_records_to_validate_or_save(association, new_record, autosave)
if new_record
association
elsif association.loaded?
autosave ? association : association.find_all { |record| record.new_record? }
elsif autosave
association.target.find_all { |record| record.new_record? || record.changed? || record.marked_for_destruction? }
else
autosave ? association.target : association.target.find_all { |record| record.new_record? }
association.target.find_all { |record| record.new_record? }
end
end

Expand Down
13 changes: 4 additions & 9 deletions activerecord/test/cases/autosave_association_test.rb
Expand Up @@ -701,23 +701,18 @@ def save(*args)

define_method("test_should_rollback_destructions_if_an_exception_occurred_while_saving_#{association_name}") do
2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
before = @pirate.send(association_name).map { |c| c }
before = @pirate.send(association_name).map { |c| c.mark_for_destruction ; c }

# Stub the save method of the first child to destroy and the second to raise an exception
class << before.first
def save(*args)
super
destroy
end
end
# Stub the destroy method of the the second child to raise an exception
class << before.last
def save(*args)
def destroy(*args)
super
raise 'Oh noes!'
end
end

assert_raise(RuntimeError) { assert !@pirate.save }
assert before.first.frozen? # the first child was indeed destroyed
assert_equal before, @pirate.reload.send(association_name)
end

Expand Down

0 comments on commit 5193fe9

Please sign in to comment.