Merge pull request #49040 from paulreece/select_hash_sql_fix
Improve #select with Hash argument to be more consistent with #where
This commit is contained in:
commit
55412cd925
@ -2002,6 +2002,10 @@ def transform_select_hash_values(fields)
|
||||
case columns_aliases
|
||||
when Hash
|
||||
columns_aliases.map do |column, column_alias|
|
||||
if values[:joins]&.include?(key)
|
||||
references = PredicateBuilder.references({ key.to_s => fields[key] })
|
||||
self.references_values |= references unless references.empty?
|
||||
end
|
||||
arel_column("#{key}.#{column}") do
|
||||
predicate_builder.resolve_arel_attribute(key.to_s, column)
|
||||
end.as(column_alias.to_s)
|
||||
|
@ -45,6 +45,21 @@ def test_select_with_hash_array_value_with_not_exists_field
|
||||
end
|
||||
end
|
||||
|
||||
def test_select_with_hash_and_table_alias
|
||||
post = Post.joins(:comments, :comments_with_extend)
|
||||
.select(
|
||||
:title,
|
||||
posts: { title: :post_title },
|
||||
comments: { body: :comment_body },
|
||||
comments_with_extend: { body: :comment_body_2 }
|
||||
)
|
||||
.take
|
||||
|
||||
assert_equal post.title, post.post_title
|
||||
assert_not_nil post.comment_body
|
||||
assert_not_nil post.comment_body_2
|
||||
end
|
||||
|
||||
def test_select_with_invalid_nested_field
|
||||
assert_raises(ActiveRecord::StatementInvalid) do
|
||||
Post.select(posts: { "UPPER(title)" => :post_title }).take
|
||||
|
Loading…
Reference in New Issue
Block a user