Ensure apply_join_dependency
for subqueries in from
and where
Fixes #21577.
This commit is contained in:
parent
a7ef60d520
commit
df49896a1e
@ -4,6 +4,10 @@ module ActiveRecord
|
||||
class PredicateBuilder
|
||||
class RelationHandler # :nodoc:
|
||||
def call(attribute, value)
|
||||
if value.eager_loading?
|
||||
value = value.send(:apply_join_dependency)
|
||||
end
|
||||
|
||||
if value.select_values.empty?
|
||||
value = value.select(value.arel_attribute(value.klass.primary_key))
|
||||
end
|
||||
|
@ -963,6 +963,9 @@ def build_from
|
||||
name = from_clause.name
|
||||
case opts
|
||||
when Relation
|
||||
if opts.eager_loading?
|
||||
opts = opts.send(:apply_join_dependency)
|
||||
end
|
||||
name ||= "subquery"
|
||||
opts.arel.as(name.to_s)
|
||||
else
|
||||
|
@ -195,6 +195,18 @@ def test_group_with_subquery_in_from_does_not_use_original_table_name
|
||||
assert_equal(relation.map(&:post_count).sort, subquery.values.sort)
|
||||
end
|
||||
|
||||
def test_finding_with_subquery_with_eager_loading_in_from
|
||||
relation = Comment.includes(:post).where("posts.type": "Post")
|
||||
assert_equal relation.to_a, Comment.select("*").from(relation).to_a
|
||||
assert_equal relation.to_a, Comment.select("subquery.*").from(relation).to_a
|
||||
assert_equal relation.to_a, Comment.select("a.*").from(relation, :a).to_a
|
||||
end
|
||||
|
||||
def test_finding_with_subquery_with_eager_loading_in_where
|
||||
relation = Comment.includes(:post).where("posts.type": "Post")
|
||||
assert_equal relation.to_a, Comment.where(id: relation).to_a
|
||||
end
|
||||
|
||||
def test_finding_with_conditions
|
||||
assert_equal ["David"], Author.where(name: "David").map(&:name)
|
||||
assert_equal ["Mary"], Author.where(["name = ?", "Mary"]).map(&:name)
|
||||
|
Loading…
Reference in New Issue
Block a user