diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-01-29 08:34:33 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-01-29 09:24:14 +0900 |
commit | 159b21b59dba120e58eeb8bff89b9d322e720c44 (patch) | |
tree | 4961855b5e5d01f0dd9332869f94539b81422caa /activerecord/lib/active_record/relation/predicate_builder.rb | |
parent | 6b8c161fc2cabdadd33826d21f9bd099041b8069 (diff) | |
download | rails-159b21b59dba120e58eeb8bff89b9d322e720c44.tar.gz rails-159b21b59dba120e58eeb8bff89b9d322e720c44.tar.bz2 rails-159b21b59dba120e58eeb8bff89b9d322e720c44.zip |
Fix expanding an array of `composed_of` objects which have multiple mappings
Follow up of #31724.
If `composed_of` objects have multiple mappings, array predicate handler
can not correctly handle the expanded condition.
We need to handle it like polymorphic association objects.
Diffstat (limited to 'activerecord/lib/active_record/relation/predicate_builder.rb')
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder.rb | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 885c26d7aa..f3286846d2 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -86,6 +86,18 @@ module ActiveRecord expand_from_hash(query).reduce(&:and) end 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)) + end + end.reduce(&:and) + end + queries.reduce(&:or) # FIXME: Deprecate this and provide a public API to force equality elsif (value.is_a?(Range) || value.is_a?(Array)) && table.type(key.to_s).respond_to?(:subtype) |