mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #14154 from al2o3cr/issue12770
Pass a base relation to build_default_scope when joining
This commit is contained in:
commit
c81e4e6a26
4 changed files with 14 additions and 4 deletions
|
@ -54,7 +54,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
scope_chain_index += 1
|
scope_chain_index += 1
|
||||||
|
|
||||||
scope_chain_items.concat [klass.send(:build_default_scope)].compact
|
scope_chain_items.concat [klass.send(:build_default_scope, ActiveRecord::Relation.create(klass, table))].compact
|
||||||
|
|
||||||
rel = scope_chain_items.inject(scope_chain_items.shift) do |left, right|
|
rel = scope_chain_items.inject(scope_chain_items.shift) do |left, right|
|
||||||
left.merge right
|
left.merge right
|
||||||
|
|
|
@ -94,14 +94,14 @@ module ActiveRecord
|
||||||
self.default_scopes += [scope]
|
self.default_scopes += [scope]
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_default_scope # :nodoc:
|
def build_default_scope(base_rel = relation) # :nodoc:
|
||||||
if !Base.is_a?(method(:default_scope).owner)
|
if !Base.is_a?(method(:default_scope).owner)
|
||||||
# The user has defined their own default scope method, so call that
|
# The user has defined their own default scope method, so call that
|
||||||
evaluate_default_scope { default_scope }
|
evaluate_default_scope { default_scope }
|
||||||
elsif default_scopes.any?
|
elsif default_scopes.any?
|
||||||
evaluate_default_scope do
|
evaluate_default_scope do
|
||||||
default_scopes.inject(relation) do |default_scope, scope|
|
default_scopes.inject(base_rel) do |default_scope, scope|
|
||||||
default_scope.merge(unscoped { scope.call })
|
default_scope.merge(base_rel.scoping { scope.call })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -117,4 +117,13 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase
|
||||||
|
|
||||||
assert_equal [author], Author.where(id: author).joins(:special_categorizations)
|
assert_equal [author], Author.where(id: author).joins(:special_categorizations)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "the default scope of the target is correctly aliased when joining associations" do
|
||||||
|
author = Author.create! name: "Jon"
|
||||||
|
author.categories.create! name: 'Not Special'
|
||||||
|
author.special_categories.create! name: 'Special'
|
||||||
|
|
||||||
|
categories = author.categories.includes(:special_categorizations).references(:special_categorizations).to_a
|
||||||
|
assert_equal 2, categories.size
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,6 +22,7 @@ class Category < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
has_many :categorizations
|
has_many :categorizations
|
||||||
|
has_many :special_categorizations
|
||||||
has_many :post_comments, :through => :posts, :source => :comments
|
has_many :post_comments, :through => :posts, :source => :comments
|
||||||
|
|
||||||
has_many :authors, :through => :categorizations
|
has_many :authors, :through => :categorizations
|
||||||
|
|
Loading…
Reference in a new issue