Skip to content

Commit

Permalink
Merge pull request #10417 from jholton/fix_association_auto_save
Browse files Browse the repository at this point in the history
autosave_association issue that occurs when table has unique index (resubmission)
Conflicts:

	activerecord/CHANGELOG.md
  • Loading branch information
jonleighton authored and senny committed Jul 15, 2013
1 parent 110a3d3 commit d635246
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## unreleased ##

* fixes bug introduced by #3329. Now, when autosaving associations,
deletions happen before inserts and saves. This prevents a 'duplicate
unique value' database error that would occur if a record being created had
the same value on a unique indexed field as that of a record being destroyed.

*Johnny Holton*

* Flatten merged join_values before building the joins.

While joining_values special treatment is given to string values.
Expand Down
17 changes: 8 additions & 9 deletions activerecord/lib/active_record/autosave_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,18 @@ def save_collection_association(reflection)
autosave = reflection.options[:autosave]

if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave)
records_to_destroy = []

if autosave
records_to_destroy = records.select(&:marked_for_destruction?)
records_to_destroy.each { |record| association.destroy(record) }
records -= records_to_destroy
end

records.each do |record|
next if record.destroyed?

saved = true

if autosave && record.marked_for_destruction?
records_to_destroy << record
elsif autosave != false && (@new_record_before_save || record.new_record?)
if autosave != false && (@new_record_before_save || record.new_record?)
if autosave
saved = association.insert_record(record, false)
else
Expand All @@ -355,10 +358,6 @@ def save_collection_association(reflection)

raise ActiveRecord::Rollback unless saved
end

records_to_destroy.each do |record|
association.destroy(record)
end
end

# reconstruct the scope now that we know the owner's id
Expand Down
22 changes: 19 additions & 3 deletions activerecord/test/cases/autosave_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def test_autosave_new_record_on_has_many_can_be_disabled_per_relationship
end

class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
self.use_transactional_fixtures = false unless supports_savepoints?
self.use_transactional_fixtures = false

def setup
super
Expand Down Expand Up @@ -764,6 +764,20 @@ def test_when_new_record_a_child_marked_for_destruction_should_not_affect_other_
assert_equal 2, @pirate.birds.reload.length
end

def test_should_save_new_record_that_has_same_value_as_existing_record_marked_for_destruction_on_field_that_has_unique_index
Bird.connection.add_index :birds, :name, unique: true

3.times { |i| @pirate.birds.create(name: "unique_birds_#{i}") }

@pirate.birds[0].mark_for_destruction
@pirate.birds.build(name: @pirate.birds[0].name)
@pirate.save!

assert_equal 3, @pirate.birds.reload.length
ensure
Bird.connection.remove_index :birds, column: :name
end

# Add and remove callbacks tests for association collections.
%w{ method proc }.each do |callback_type|
define_method("test_should_run_add_callback_#{callback_type}s_for_has_many") do
Expand Down Expand Up @@ -846,8 +860,10 @@ def test_a_child_marked_for_destruction_should_not_be_destroyed_twice_while_savi
@pirate.parrots.each { |parrot| parrot.mark_for_destruction }
assert @pirate.save

assert_queries(0) do
assert @pirate.save
Pirate.transaction do
assert_queries(0) do
assert @pirate.save
end
end
end

Expand Down

0 comments on commit d635246

Please sign in to comment.