aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-06-05 00:27:43 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-06-07 06:58:45 +0900
commit5dc72378b783e924c5bf079ca660388ec4ac9224 (patch)
tree5411c32f4330fca8ea97c56b810ea77cd079983a /activerecord
parent48c95cf7e591ed3b683705ded92c6d7c7518aaec (diff)
downloadrails-5dc72378b783e924c5bf079ca660388ec4ac9224.tar.gz
rails-5dc72378b783e924c5bf079ca660388ec4ac9224.tar.bz2
rails-5dc72378b783e924c5bf079ca660388ec4ac9224.zip
Fix `collection.create` to could be rolled back by `after_save`
In `_create_record`, explicit `transaction` block requires rollback handling manually when `insert_record` is failed. We need to handle it in `_create_record`, not in `insert_record`, since our test cases expect a record added to target and returned even if `insert_record` is failed, Closes #31488.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb12
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb11
-rw-r--r--activerecord/test/models/company.rb10
3 files changed, 30 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index 7f1df9a21d..48cb819484 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -358,14 +358,18 @@ module ActiveRecord
if attributes.is_a?(Array)
attributes.collect { |attr| _create_record(attr, raise, &block) }
else
+ record = build_record(attributes, &block)
transaction do
- add_to_target(build_record(attributes, &block)) do |record|
- insert_record(record, true, raise) {
+ result = nil
+ add_to_target(record) do
+ result = insert_record(record, true, raise) {
@_was_loaded = loaded?
@association_ids = nil
}
end
+ raise ActiveRecord::Rollback unless result
end
+ record
end
end
@@ -443,7 +447,9 @@ module ActiveRecord
end
end
- result && records
+ raise ActiveRecord::Rollback unless result
+
+ records
end
def replace_on_target(record, index, skip_callbacks)
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index cc8f33f142..0ca902385a 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -2705,6 +2705,17 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
end
+ def test_create_children_could_be_rolled_back_by_after_save
+ firm = Firm.create!(name: "A New Firm, Inc")
+ assert_no_difference "Client.count" do
+ client = firm.clients.create(name: "New Client") do |cli|
+ cli.rollback_on_save = true
+ assert_not cli.rollback_on_create_called
+ end
+ assert client.rollback_on_create_called
+ end
+ end
+
private
def force_signal37_to_load_all_clients_of_firm
diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb
index 6219f57fa1..d4d5275b78 100644
--- a/activerecord/test/models/company.rb
+++ b/activerecord/test/models/company.rb
@@ -150,6 +150,16 @@ class Client < Company
throw :abort if throw_on_save
end
+ attr_accessor :rollback_on_save
+ after_save do
+ raise ActiveRecord::Rollback if rollback_on_save
+ end
+
+ attr_accessor :rollback_on_create_called
+ after_rollback(on: :create) do |client|
+ client.rollback_on_create_called = true
+ end
+
class RaisedOnDestroy < RuntimeError; end
attr_accessor :raise_on_destroy
before_destroy do