diff options
author | Dylan Thacker-Smith <Dylan.Smith@shopify.com> | 2019-01-17 13:33:30 -0500 |
---|---|---|
committer | Dylan Thacker-Smith <Dylan.Smith@shopify.com> | 2019-01-17 13:37:11 -0500 |
commit | 242fc54f57d8db96eb2d34b1db19b9a61d7412b7 (patch) | |
tree | dde995ef2f3a1e5aef63ed37e2fcf3ae41c48fd7 /activerecord/lib | |
parent | 2dee59fed1e78b983aed4db53dc8fc59e49b9200 (diff) | |
download | rails-242fc54f57d8db96eb2d34b1db19b9a61d7412b7.tar.gz rails-242fc54f57d8db96eb2d34b1db19b9a61d7412b7.tar.bz2 rails-242fc54f57d8db96eb2d34b1db19b9a61d7412b7.zip |
activerecord: Use a simpler query condition for aggregates with one mapping
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder.rb | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index b59ff912fe..b8d64be343 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -90,16 +90,21 @@ module ActiveRecord queries.reduce(&:or) elsif table.aggregated_with?(key) mapping = table.reflect_on_aggregation(key).mapping - queries = Array.wrap(value).map do |object| - mapping.map do |field_attr, aggregate_attr| - if mapping.size == 1 && !object.respond_to?(aggregate_attr) - build(table.arel_attribute(field_attr), object) - else - build(table.arel_attribute(field_attr), object.send(aggregate_attr)) + if mapping.length == 1 + mapping.first.yield_self do |field_attr, aggregate_attr| + values = Array.wrap(value).map do |object| + object.respond_to?(aggregate_attr) ? object.send(aggregate_attr) : object end - end.reduce(&:and) + build(table.arel_attribute(field_attr), values) + end + else + queries = Array.wrap(value).map do |object| + mapping.map do |field_attr, aggregate_attr| + build(table.arel_attribute(field_attr), object.send(aggregate_attr)) + end.reduce(&:and) + end + queries.reduce(&:or) end - queries.reduce(&:or) else build(table.arel_attribute(key), value) end |