mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #31724 from orekyuu/fix-expand-composed-object-array
Fix not expanded problem when passing an Array object as argument to the where method using composed_of column.
This commit is contained in:
commit
1c239df3de
3 changed files with 37 additions and 3 deletions
|
@ -1,3 +1,17 @@
|
||||||
|
* Fix not expanded problem when passing an Array object as argument to the where method using `composed_of` column.
|
||||||
|
|
||||||
|
```
|
||||||
|
david_balance = customers(:david).balance
|
||||||
|
Customer.where(balance: [david_balance]).to_sql
|
||||||
|
|
||||||
|
# Before: WHERE `customers`.`balance` = NULL
|
||||||
|
# After : WHERE `customers`.`balance` = 50
|
||||||
|
```
|
||||||
|
|
||||||
|
Fixes #31723.
|
||||||
|
|
||||||
|
*Yutaro Kanagawa*
|
||||||
|
|
||||||
* Fix `count(:all)` with eager loading and having an order other than the driving table.
|
* Fix `count(:all)` with eager loading and having an order other than the driving table.
|
||||||
|
|
||||||
Fixes #31783.
|
Fixes #31783.
|
||||||
|
|
|
@ -155,10 +155,12 @@ module ActiveRecord
|
||||||
if aggregation = reflect_on_aggregation(attr.to_sym)
|
if aggregation = reflect_on_aggregation(attr.to_sym)
|
||||||
mapping = aggregation.mapping
|
mapping = aggregation.mapping
|
||||||
mapping.each do |field_attr, aggregate_attr|
|
mapping.each do |field_attr, aggregate_attr|
|
||||||
if mapping.size == 1 && !value.respond_to?(aggregate_attr)
|
expanded_attrs[field_attr] = if value.is_a?(Array)
|
||||||
expanded_attrs[field_attr] = value
|
value.map { |it| it.send(aggregate_attr) }
|
||||||
|
elsif mapping.size == 1 && !value.respond_to?(aggregate_attr)
|
||||||
|
value
|
||||||
else
|
else
|
||||||
expanded_attrs[field_attr] = value.send(aggregate_attr)
|
value.send(aggregate_attr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
|
|
@ -868,6 +868,24 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
assert_equal customers(:david), found_customer
|
assert_equal customers(:david), found_customer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_hash_condition_find_with_aggregate_having_three_mapping_array
|
||||||
|
david_address = customers(:david).address
|
||||||
|
zaphod_address = customers(:zaphod).address
|
||||||
|
assert_kind_of Address, david_address
|
||||||
|
assert_kind_of Address, zaphod_address
|
||||||
|
found_customers = Customer.where(address: [david_address, zaphod_address])
|
||||||
|
assert_equal [customers(:david), customers(:zaphod)], found_customers
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_hash_condition_find_with_aggregate_having_one_mapping_array
|
||||||
|
david_balance = customers(:david).balance
|
||||||
|
zaphod_balance = customers(:zaphod).balance
|
||||||
|
assert_kind_of Money, david_balance
|
||||||
|
assert_kind_of Money, zaphod_balance
|
||||||
|
found_customers = Customer.where(balance: [david_balance, zaphod_balance])
|
||||||
|
assert_equal [customers(:david), customers(:zaphod)], found_customers
|
||||||
|
end
|
||||||
|
|
||||||
def test_hash_condition_find_with_aggregate_attribute_having_same_name_as_field_and_key_value_being_aggregate
|
def test_hash_condition_find_with_aggregate_attribute_having_same_name_as_field_and_key_value_being_aggregate
|
||||||
gps_location = customers(:david).gps_location
|
gps_location = customers(:david).gps_location
|
||||||
assert_kind_of GpsLocation, gps_location
|
assert_kind_of GpsLocation, gps_location
|
||||||
|
|
Loading…
Reference in a new issue