diff options
author | Dylan Thacker-Smith <Dylan.Smith@shopify.com> | 2019-01-18 10:03:46 -0500 |
---|---|---|
committer | Dylan Thacker-Smith <Dylan.Smith@shopify.com> | 2019-01-18 11:10:10 -0500 |
commit | 592a961f299a672abbf17a9d01dea1ded5c4e35b (patch) | |
tree | 900c5b2763b4811c45f9269b7b13ea4a060cce77 /activerecord/lib | |
parent | ff3d1a42d5800c999be7351b72317cefb5961ee8 (diff) | |
download | rails-592a961f299a672abbf17a9d01dea1ded5c4e35b.tar.gz rails-592a961f299a672abbf17a9d01dea1ded5c4e35b.tar.bz2 rails-592a961f299a672abbf17a9d01dea1ded5c4e35b.zip |
activerecord: Fix where nil condition on composed_of attribute
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder.rb | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index a0f6ada3ff..240de3bb69 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -90,16 +90,17 @@ module ActiveRecord queries.reduce(&:or) elsif table.aggregated_with?(key) mapping = table.reflect_on_aggregation(key).mapping - if mapping.length == 1 + values = value.nil? ? [nil] : Array.wrap(value) + if mapping.length == 1 || values.empty? column_name, aggr_attr = mapping.first - values = Array.wrap(value).map do |object| + values = values.map do |object| object.respond_to?(aggr_attr) ? object.public_send(aggr_attr) : object end build(table.arel_attribute(column_name), values) else - queries = Array.wrap(value).map do |object| + queries = values.map do |object| mapping.map do |field_attr, aggregate_attr| - build(table.arel_attribute(field_attr), object.send(aggregate_attr)) + build(table.arel_attribute(field_attr), object.try!(aggregate_attr)) end.reduce(&:and) end queries.reduce(&:or) |