Skip to content
Permalink
Browse files

Allow returning nil for `default_scope`

  • Loading branch information...
kamipo committed Feb 27, 2019
1 parent 672c6ca commit 634953af94f8677f9fd3b07b9560139f50d29467
Showing with 5 additions and 5 deletions.
  1. +4 −5 activerecord/lib/active_record/scoping/default.rb
  2. +1 −0 activerecord/test/models/developer.rb
@@ -100,7 +100,7 @@ def default_scope(scope = nil) # :doc:
self.default_scopes += [scope]
end

def build_default_scope(base_rel = nil)
def build_default_scope(relation = relation())
return if abstract_class?

if default_scope_override.nil?
@@ -111,15 +111,14 @@ def build_default_scope(base_rel = nil)
# 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)
relation.merge!(scope)
end
end
elsif default_scopes.any?
base_rel ||= relation
evaluate_default_scope do
default_scopes.inject(base_rel) do |default_scope, scope|
default_scopes.inject(relation) do |default_scope, scope|
scope = scope.respond_to?(:to_proc) ? scope : scope.method(:call)
default_scope.merge!(base_rel.instance_exec(&scope))
default_scope.instance_exec(&scope) || default_scope
end
end
end
@@ -207,6 +207,7 @@ class InheritedPoorDeveloperCalledJamis < DeveloperCalledJamis
class MultiplePoorDeveloperCalledJamis < ActiveRecord::Base
self.table_name = "developers"

default_scope { }
default_scope -> { where(name: "Jamis") }
default_scope -> { where(salary: 50000) }
end

0 comments on commit 634953a

Please sign in to comment.
You can’t perform that action at this time.