Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5-1-stable: Fix unscoping default_scope in STI associations #29877

Merged
merged 2 commits into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ def reflection_scope
@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.unscoped

Expand Down Expand Up @@ -162,7 +172,7 @@ def build_scope
end

scope.unscope_values = Array(values[:unscope]) + Array(preload_values[:unscope])
klass.default_scoped.merge(scope)
klass_scope.merge(scope)
end
end
end
Expand Down
12 changes: 5 additions & 7 deletions activerecord/lib/active_record/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,12 @@ def join_scopes(table, predicate_builder) # :nodoc:
end

def klass_join_scope(table, predicate_builder) # :nodoc:
if klass.current_scope && klass.current_scope.values.empty?
klass.unscoped
relation = ActiveRecord::Relation.create(klass, table, predicate_builder)
current_scope = klass.current_scope

if current_scope && current_scope.empty_scope?
relation
else
relation = ActiveRecord::Relation.create(
klass,
table,
predicate_builder,
)
klass.send(:build_default_scope, relation)
end
end
Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,10 @@ def inspect
"#<#{self.class.name} [#{entries.join(', ')}]>"
end

def empty_scope? # :nodoc:
@values == klass.unscoped.values
end

protected

def load_records(records)
Expand Down
18 changes: 18 additions & 0 deletions activerecord/test/cases/scoping/default_scoping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,24 @@ def test_unscoped_with_joins_should_not_have_default_scope
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
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def to_s
end

class SpecialComment < Comment
default_scope { where(deleted_at: nil) }
end

class SubSpecialComment < SpecialComment
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
t.string :resource_id
t.string :resource_type
t.integer :developer_id
t.datetime :deleted_at
end

create_table :companies, force: true do |t|
Expand Down