diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2017-07-18 13:11:01 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-18 13:11:01 -0400 |
commit | 41d51b07160484b616e48658ddb76d1d1d13a6c0 (patch) | |
tree | 8667114fb9c227ad1459782f8405583828040a2f /activerecord | |
parent | 8ebe1f2feed30809abb3f114242dda7379e66e4b (diff) | |
parent | 9aa04315febfb37b50f52471a2837c40313a2d5f (diff) | |
download | rails-41d51b07160484b616e48658ddb76d1d1d13a6c0.tar.gz rails-41d51b07160484b616e48658ddb76d1d1d13a6c0.tar.bz2 rails-41d51b07160484b616e48658ddb76d1d1d13a6c0.zip |
Merge pull request #29834 from kamipo/fix_unscoping_default_scope_with_sti_association
Fix unscoping `default_scope` in STI associations
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/association.rb | 12 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/scoping/default_scoping_test.rb | 18 | ||||
-rw-r--r-- | activerecord/test/models/comment.rb | 1 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 1 |
6 files changed, 39 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index 85343040db..698fd29beb 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -114,8 +114,18 @@ module ActiveRecord @reflection_scope ||= reflection.scope_for(klass) end + def klass_scope + current_scope = klass.current_scope + + if current_scope && current_scope.empty_scope? + klass.unscoped + else + klass.default_scoped + end + end + def build_scope - scope = klass.default_scoped + scope = klass_scope if reflection.type scope.where!(reflection.type => model.base_class.sti_name) diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 4f53280b5c..1026e20f79 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -219,8 +219,10 @@ module ActiveRecord end def klass_join_scope(table, predicate_builder) # :nodoc: - if klass.current_scope && klass.current_scope.values.empty? - klass.unscoped + current_scope = klass.current_scope + + if current_scope && current_scope.empty_scope? + build_scope(table, predicate_builder) else klass.default_scoped(build_scope(table, predicate_builder)) end diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index c5d98e970a..d9e1e5c39c 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -642,6 +642,10 @@ module ActiveRecord "#<#{self.class.name} [#{entries.join(', ')}]>" end + def empty_scope? # :nodoc: + @values == klass.unscoped.values + end + protected def load_records(records) diff --git a/activerecord/test/cases/scoping/default_scoping_test.rb b/activerecord/test/cases/scoping/default_scoping_test.rb index a5061cfce7..a8c79628e7 100644 --- a/activerecord/test/cases/scoping/default_scoping_test.rb +++ b/activerecord/test/cases/scoping/default_scoping_test.rb @@ -392,6 +392,24 @@ class DefaultScopingTest < ActiveRecord::TestCase Comment.joins(:post).to_a end + def test_sti_association_with_unscoped_not_affected_by_default_scope + post = posts(:thinking) + comments = [comments(:does_it_hurt)] + + post.special_comments.update_all(deleted_at: Time.now) + + assert_raises(ActiveRecord::RecordNotFound) { Post.joins(:special_comments).find(post.id) } + assert_equal [], post.special_comments + + SpecialComment.unscoped do + assert_equal post, Post.joins(:special_comments).find(post.id) + assert_equal comments, Post.joins(:special_comments).find(post.id).special_comments + assert_equal comments, Post.eager_load(:special_comments).find(post.id).special_comments + assert_equal comments, Post.includes(:special_comments).find(post.id).special_comments + assert_equal comments, Post.preload(:special_comments).find(post.id).special_comments + end + end + def test_default_scope_select_ignored_by_aggregations assert_equal DeveloperWithSelect.all.to_a.count, DeveloperWithSelect.count end diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index eecf923046..dc2d421cd7 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -54,6 +54,7 @@ class Comment < ActiveRecord::Base end class SpecialComment < Comment + default_scope { where(deleted_at: nil) } end class SubSpecialComment < SpecialComment diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index f534e9c00e..90185ff6c5 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -189,6 +189,7 @@ ActiveRecord::Schema.define do t.string :resource_id t.string :resource_type t.integer :developer_id + t.datetime :deleted_at end create_table :companies, force: true do |t| |