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

Fix default_scoped with defined default_scope on STI model #29293

Merged
merged 1 commit into from
Jun 1, 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
6 changes: 5 additions & 1 deletion activerecord/lib/active_record/scoping/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ def build_default_scope(base_rel = nil)

if default_scope_override
# The user has defined their own default scope method, so call that
evaluate_default_scope { default_scope }
evaluate_default_scope do
if scope = default_scope
(base_rel ||= relation).merge(scope)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I changed to merge base_rel without scoping. How about it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

Can you think of a way to show the problem (well.. difference, at least) with the other version in a test? I thought I understood what was going on, but couldn't come up with code that proved it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I added an extra test in 6e390e2.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... 6e390e2 passed in my local but failed in CI...
https://travis-ci.org/rails/rails/jobs/237857702#L437

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that test have failed on the previous version of this PR? 😕

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it has failed. The previous version of this PR overwrites current_scope when build_default_scope is already affected in scoping.

% ARCONN=sqlite3 be ruby -w -Itest test/cases/inheritance_test.rb
Using sqlite3
Run options: --seed 45762

# Running:

........F.......................................................

Finished in 0.576700s, 110.9762 runs/s, 235.8245 assertions/s.

  1) Failure:
InheritanceTest#test_inheritance_with_default_scope_respects_scoping [test/cases/inheritance_test.rb:446]:
Expected: 0
  Actual: 1

64 runs, 136 assertions, 1 failures, 0 errors, 0 skips
diff --git a/activerecord/lib/active_record/scoping/default.rb b/activerecord/lib/active_record/scoping/default.rb
index 70b2693b28..7f14daac70 100644
--- a/activerecord/lib/active_record/scoping/default.rb
+++ b/activerecord/lib/active_record/scoping/default.rb
@@ -108,9 +108,7 @@ def build_default_scope(base_rel = nil)
             if default_scope_override
               # The user has defined their own default scope method, so call that
               evaluate_default_scope do
-                if scope = default_scope
-                  (base_rel ||= relation).merge(scope)
-                end
+                default_scope
               end
             elsif default_scopes.any?
               base_rel ||= relation
diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb
index a61fdd6454..7fb198d913 100644
--- a/activerecord/lib/active_record/scoping/named.rb
+++ b/activerecord/lib/active_record/scoping/named.rb
@@ -31,7 +31,7 @@ def all
 
         def default_scoped # :nodoc:
           scope = relation
-          build_default_scope(scope) || scope
+          scope.scoping { build_default_scope(scope) || scope }
         end
 
         def default_extensions # :nodoc:

end
end
elsif default_scopes.any?
base_rel ||= relation
evaluate_default_scope do
Expand Down
7 changes: 6 additions & 1 deletion activerecord/test/cases/inheritance_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "cases/helper"
require "models/author"
require "models/company"
require "models/membership"
require "models/person"
require "models/post"
require "models/project"
Expand Down Expand Up @@ -29,7 +30,7 @@ def assign_store_full_sti_class(flag)

class InheritanceTest < ActiveRecord::TestCase
include InheritanceTestHelper
fixtures :companies, :projects, :subscribers, :accounts, :vegetables
fixtures :companies, :projects, :subscribers, :accounts, :vegetables, :memberships

def test_class_with_store_full_sti_class_returns_full_name
with_store_full_sti_class do
Expand Down Expand Up @@ -435,6 +436,10 @@ def test_scope_inherited_properly
assert_nothing_raised { Company.of_first_firm }
assert_nothing_raised { Client.of_first_firm }
end

def test_inheritance_with_default_scope
assert_equal 1, SelectedMembership.count(:all)
end
end

class InheritanceComputeTypeTest < ActiveRecord::TestCase
Expand Down