mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #18109 from k0kubun/unscoped-joins
Allow `joins` to be unscoped Fixes #13775
This commit is contained in:
commit
6fedc7d56a
4 changed files with 29 additions and 6 deletions
|
@ -1,3 +1,7 @@
|
|||
* Allow `joins` to be unscoped.
|
||||
|
||||
Closes #13775.
|
||||
|
||||
* Add ActiveRecord `#second_to_last` and `#third_to_last` methods.
|
||||
|
||||
*Brian Christian*
|
||||
|
|
|
@ -54,12 +54,18 @@ module ActiveRecord
|
|||
end
|
||||
scope_chain_index += 1
|
||||
|
||||
relation = ActiveRecord::Relation.create(
|
||||
klass,
|
||||
table,
|
||||
predicate_builder,
|
||||
)
|
||||
scope_chain_items.concat [klass.send(:build_default_scope, relation)].compact
|
||||
klass_scope =
|
||||
if klass.current_scope
|
||||
klass.current_scope.clone
|
||||
else
|
||||
relation = ActiveRecord::Relation.create(
|
||||
klass,
|
||||
table,
|
||||
predicate_builder,
|
||||
)
|
||||
klass.send(:build_default_scope, relation)
|
||||
end
|
||||
scope_chain_items.concat [klass_scope].compact
|
||||
|
||||
rel = scope_chain_items.inject(scope_chain_items.shift) do |left, right|
|
||||
left.merge right
|
||||
|
|
|
@ -374,6 +374,18 @@ class DefaultScopingTest < ActiveRecord::TestCase
|
|||
assert_equal 10, DeveloperCalledJamis.unscoped { DeveloperCalledJamis.poor }.length
|
||||
end
|
||||
|
||||
def test_default_scope_with_joins
|
||||
assert_equal Comment.where(post_id: SpecialPostWithDefaultScope.pluck(:id)).count,
|
||||
Comment.joins(:special_post_with_default_scope).count
|
||||
assert_equal Comment.where(post_id: Post.pluck(:id)).count,
|
||||
Comment.joins(:post).count
|
||||
end
|
||||
|
||||
def test_unscoped_with_joins_should_not_have_default_scope
|
||||
assert_equal SpecialPostWithDefaultScope.unscoped { Comment.joins(:special_post_with_default_scope).to_a },
|
||||
Comment.joins(:post).to_a
|
||||
end
|
||||
|
||||
def test_default_scope_select_ignored_by_aggregations
|
||||
assert_equal DeveloperWithSelect.all.to_a.count, DeveloperWithSelect.count
|
||||
end
|
||||
|
|
|
@ -14,6 +14,7 @@ class Comment < ActiveRecord::Base
|
|||
has_many :ratings
|
||||
|
||||
belongs_to :first_post, :foreign_key => :post_id
|
||||
belongs_to :special_post_with_default_scope, foreign_key: :post_id
|
||||
|
||||
has_many :children, :class_name => 'Comment', :foreign_key => :parent_id
|
||||
belongs_to :parent, :class_name => 'Comment', :counter_cache => :children_count
|
||||
|
|
Loading…
Reference in a new issue