From c9822ace4c8961336527670e7eef59093f515dfd Mon Sep 17 00:00:00 2001 From: orekyuu Date: Fri, 26 Jan 2018 10:29:41 +0900 Subject: Fix not expanded problem when passing an Array object as argument to the where method using composed_of column. Fixes #31723 ``` david_balance = customers(:david).balance Customer.where(balance: [david_balance]).to_sql # Before: WHERE `customers`.`balance` = NULL # After : WHERE `customers`.`balance` = 50 ``` --- activerecord/lib/active_record/sanitization.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb index 58da106092..4acc491104 100644 --- a/activerecord/lib/active_record/sanitization.rb +++ b/activerecord/lib/active_record/sanitization.rb @@ -155,10 +155,14 @@ module ActiveRecord if aggregation = reflect_on_aggregation(attr.to_sym) mapping = aggregation.mapping mapping.each do |field_attr, aggregate_attr| - if mapping.size == 1 && !value.respond_to?(aggregate_attr) - expanded_attrs[field_attr] = value + expanded_attrs[field_attr] = if mapping.size == 1 && !value.respond_to?(aggregate_attr) + if value.is_a?(Array) + value.map { |it| it.respond_to?(aggregate_attr) ? it.send(aggregate_attr) : it } + else + value + end else - expanded_attrs[field_attr] = value.send(aggregate_attr) + value.send(aggregate_attr) end end else -- cgit v1.2.3 From 41d34ae68fc09474d88ed286890f4e73e815fb66 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 29 Jan 2018 06:45:10 +0900 Subject: Allow expanding an array of `composed_of` objects --- activerecord/lib/active_record/sanitization.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb index 4acc491104..173794b8f4 100644 --- a/activerecord/lib/active_record/sanitization.rb +++ b/activerecord/lib/active_record/sanitization.rb @@ -155,12 +155,10 @@ module ActiveRecord if aggregation = reflect_on_aggregation(attr.to_sym) mapping = aggregation.mapping mapping.each do |field_attr, aggregate_attr| - expanded_attrs[field_attr] = if mapping.size == 1 && !value.respond_to?(aggregate_attr) - if value.is_a?(Array) - value.map { |it| it.respond_to?(aggregate_attr) ? it.send(aggregate_attr) : it } - else - value - end + expanded_attrs[field_attr] = if value.is_a?(Array) + value.map { |it| it.send(aggregate_attr) } + elsif mapping.size == 1 && !value.respond_to?(aggregate_attr) + value else value.send(aggregate_attr) end -- cgit v1.2.3