aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-02-07 23:18:41 +0900
committerGitHub <noreply@github.com>2019-02-07 23:18:41 +0900
commitb67d5c6dedbf033515a96a95d24d085bf99a0d07 (patch)
tree634c8632646c2a4aa2560df46fad9f0a3e184ffb /activerecord/test
parent2e018361c7c51e36d1d98bf770b7456d78dee68b (diff)
parent22360534ac922c68fb0a28f584b48bc0f3633221 (diff)
downloadrails-b67d5c6dedbf033515a96a95d24d085bf99a0d07.tar.gz
rails-b67d5c6dedbf033515a96a95d24d085bf99a0d07.tar.bz2
rails-b67d5c6dedbf033515a96a95d24d085bf99a0d07.zip
Merge pull request #35186 from kamipo/fix_leaking_scope_on_relation_create
Fix `relation.create` to avoid leaking scope to initialization block and callbacks
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/base_test.rb2
-rw-r--r--activerecord/test/cases/relations_test.rb21
-rw-r--r--activerecord/test/models/bird.rb5
3 files changed, 24 insertions, 4 deletions
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 4938b6865f..363593ca19 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -1535,7 +1535,7 @@ class BasicsTest < ActiveRecord::TestCase
Bird.create!(name: "Bluejay")
ActiveRecord::Base.connection.while_preventing_writes do
- assert_queries(2) { Bird.where(name: "Bluejay").explain }
+ assert_nothing_raised { Bird.where(name: "Bluejay").explain }
end
end
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 6b5b877260..0ab0459c38 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -1167,7 +1167,12 @@ class RelationTest < ActiveRecord::TestCase
end
def test_first_or_create_with_block
- parrot = Bird.where(color: "green").first_or_create { |bird| bird.name = "parrot" }
+ canary = Bird.create!(color: "yellow", name: "canary")
+ parrot = Bird.where(color: "green").first_or_create do |bird|
+ bird.name = "parrot"
+ assert_equal canary, Bird.find_by!(name: "canary")
+ end
+ assert_equal 1, parrot.total_count
assert_kind_of Bird, parrot
assert_predicate parrot, :persisted?
assert_equal "green", parrot.color
@@ -1209,7 +1214,12 @@ class RelationTest < ActiveRecord::TestCase
end
def test_first_or_create_bang_with_valid_block
- parrot = Bird.where(color: "green").first_or_create! { |bird| bird.name = "parrot" }
+ canary = Bird.create!(color: "yellow", name: "canary")
+ parrot = Bird.where(color: "green").first_or_create! do |bird|
+ bird.name = "parrot"
+ assert_equal canary, Bird.find_by!(name: "canary")
+ end
+ assert_equal 1, parrot.total_count
assert_kind_of Bird, parrot
assert_predicate parrot, :persisted?
assert_equal "green", parrot.color
@@ -1259,7 +1269,12 @@ class RelationTest < ActiveRecord::TestCase
end
def test_first_or_initialize_with_block
- parrot = Bird.where(color: "green").first_or_initialize { |bird| bird.name = "parrot" }
+ canary = Bird.create!(color: "yellow", name: "canary")
+ parrot = Bird.where(color: "green").first_or_initialize do |bird|
+ bird.name = "parrot"
+ assert_equal canary, Bird.find_by!(name: "canary")
+ end
+ assert_equal 1, parrot.total_count
assert_kind_of Bird, parrot
assert_not_predicate parrot, :persisted?
assert_predicate parrot, :valid?
diff --git a/activerecord/test/models/bird.rb b/activerecord/test/models/bird.rb
index cfefa555b3..c9f6759c7d 100644
--- a/activerecord/test/models/bird.rb
+++ b/activerecord/test/models/bird.rb
@@ -16,4 +16,9 @@ class Bird < ActiveRecord::Base
def cancel_save_callback_method
throw(:abort)
end
+
+ attr_accessor :total_count
+ after_initialize do
+ self.total_count = Bird.count
+ end
end