diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-11-06 08:29:40 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-06 08:29:40 +0900 |
commit | ffc1ed9bd54f3cf746f7ddf798e000fc025edef6 (patch) | |
tree | 899877d845bcc4a6d0cd0c3b73070801205faa16 | |
parent | e0bef22665f93e88f6b2f3ac6bd55543ed0d6343 (diff) | |
download | rails-ffc1ed9bd54f3cf746f7ddf798e000fc025edef6.tar.gz rails-ffc1ed9bd54f3cf746f7ddf798e000fc025edef6.tar.bz2 rails-ffc1ed9bd54f3cf746f7ddf798e000fc025edef6.zip |
`scoping` should respect current class and STI constraint (#29199)
A relation includes `klass`, so it can not be used as it is if current
class is different from `current_scope.klass`. It should be created new
relation by current class to respect the klass and STI constraint.
Fixes #17603.
Fixes #23576.
-rw-r--r-- | activerecord/lib/active_record/scoping/named.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/scoping/relation_scoping_test.rb | 14 | ||||
-rw-r--r-- | activerecord/test/models/comment.rb | 4 |
3 files changed, 25 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index 6fa096c1fe..310af72c41 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -24,8 +24,14 @@ module ActiveRecord # You can define a scope that applies to all finders using # {default_scope}[rdoc-ref:Scoping::Default::ClassMethods#default_scope]. def all + current_scope = self.current_scope + if current_scope - current_scope.clone + if self == current_scope.klass + current_scope.clone + else + relation.merge!(current_scope) + end else default_scoped end diff --git a/activerecord/test/cases/scoping/relation_scoping_test.rb b/activerecord/test/cases/scoping/relation_scoping_test.rb index f3b84d88c2..116f8e83aa 100644 --- a/activerecord/test/cases/scoping/relation_scoping_test.rb +++ b/activerecord/test/cases/scoping/relation_scoping_test.rb @@ -240,6 +240,20 @@ class RelationScopingTest < ActiveRecord::TestCase assert_nil SpecialComment.current_scope end + def test_scoping_respects_current_class + Comment.unscoped do + assert_equal "a comment...", Comment.all.what_are_you + assert_equal "a special comment...", SpecialComment.all.what_are_you + end + end + + def test_scoping_respects_sti_constraint + Comment.unscoped do + assert_equal comments(:greetings), Comment.find(1) + assert_raises(ActiveRecord::RecordNotFound) { SpecialComment.find(1) } + end + end + def test_circular_joins_with_scoping_does_not_crash posts = Post.joins(comments: :post).scoping do Post.first(10) diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 740aa593ac..5ab433f2d9 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -60,6 +60,10 @@ end class SpecialComment < Comment default_scope { where(deleted_at: nil) } + + def self.what_are_you + "a special comment..." + end end class SubSpecialComment < SpecialComment |