File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed
lib/active_record/relation Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -2002,6 +2002,10 @@ def transform_select_hash_values(fields)
2002
2002
case columns_aliases
2003
2003
when Hash
2004
2004
columns_aliases . map do |column , column_alias |
2005
+ if values [ :joins ] &.include? ( key )
2006
+ references = PredicateBuilder . references ( { key . to_s => fields [ key ] } )
2007
+ self . references_values |= references unless references . empty?
2008
+ end
2005
2009
arel_column ( "#{ key } .#{ column } " ) do
2006
2010
predicate_builder . resolve_arel_attribute ( key . to_s , column )
2007
2011
end . as ( column_alias . to_s )
Original file line number Diff line number Diff line change @@ -45,6 +45,21 @@ def test_select_with_hash_array_value_with_not_exists_field
45
45
end
46
46
end
47
47
48
+ def test_select_with_hash_and_table_alias
49
+ post = Post . joins ( :comments , :comments_with_extend )
50
+ . select (
51
+ :title ,
52
+ posts : { title : :post_title } ,
53
+ comments : { body : :comment_body } ,
54
+ comments_with_extend : { body : :comment_body_2 }
55
+ )
56
+ . take
57
+
58
+ assert_equal post . title , post . post_title
59
+ assert_not_nil post . comment_body
60
+ assert_not_nil post . comment_body_2
61
+ end
62
+
48
63
def test_select_with_invalid_nested_field
49
64
assert_raises ( ActiveRecord ::StatementInvalid ) do
50
65
Post . select ( posts : { "UPPER(title)" => :post_title } ) . take
You can’t perform that action at this time.
0 commit comments