Skip to content

Commit fae425b

Browse files
authored
Merge pull request rails#53247 from fatkodima/fix-cte-single-item-arrays
Fix CTE construction when passed arrays with single items
2 parents 404c764 + c857cc6 commit fae425b

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,8 @@ def build_with_expression_from_value(value, nested = false)
19211921
end
19221922
when Arel::SelectManager then value
19231923
when Array
1924+
return build_with_expression_from_value(value.first, false) if value.size == 1
1925+
19241926
parts = value.map do |query|
19251927
build_with_expression_from_value(query, true)
19261928
end

activerecord/test/cases/relation/with_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ def test_with_when_passing_arrays
7474
assert_equal (SPECIAL_POSTS + POSTS_WITH_TAGS + POSTS_WITH_COMMENTS).sort, relation.order(:id).pluck(:id)
7575
end
7676

77+
def test_with_when_passing_single_item_array
78+
relation = Post
79+
.with(posts_with_special_type_or_tags_or_comments: [Post.where(type: "SpecialPost")])
80+
.from("posts_with_special_type_or_tags_or_comments AS posts")
81+
82+
assert_equal SPECIAL_POSTS.sort, relation.order(:id).pluck(:id)
83+
end
84+
7785
def test_with_recursive
7886
top_companies = Company.where(firm_id: nil).to_a
7987
child_companies = Company.where(firm_id: top_companies).to_a

0 commit comments

Comments
 (0)