Hey all,
Ranges on single-mapping composed_of aggregates were fixed in
https://github.com/rails/rails/pull/59013,
so a value object that maps to one column now works:
Customer.where(balance: Money.new(150)..Money.new(250))
# => balance BETWEEN 150 AND 250
I left out composite aggregates deliberately and wanted to discuss how they should be handled.
What happens now
With a value object spread over more than one column, scalar and array conditions work but a range raises:
class Ingredient < ActiveRecord::Base
composed_of :quantity, class_name: "Measurement",
mapping: { quantity_value: :value, quantity_unit: :unit }
end
Ingredient.where(quantity: Measurement.new(5, "kg"))
# => quantity_value = 5.0 AND quantity_unit = 'kg'
Ingredient.where(quantity: [Measurement.new(5, "kg")])
# => quantity_value = 5.0 AND quantity_unit = 'kg'
Ingredient.where(quantity: Measurement.new(1, "kg")..Measurement.new(10, "kg"))
# => NoMethodError: undefined method 'value' for an instance of Range
The last example raises cause the multi-mapping branch of PredicateBuilder#expand_from_hash calls
object.try!(aggregate_attr) for each mapped column, and a Range responds to none of
them.
Why address this?
Now that the single-mapping case works, the multi-mapping path fails. I believe fixing it will support a more consistent API, and better Developer Experience.
Also nothing in the error points towards the aggregate so it is tricky to work out backwards that ranges aren’t supported.
How to address this?
I’m thinking let the declaration name the column a range compares against:
composed_of :quantity, class_name: "Measurement",
mapping: { quantity_value: :value, quantity_unit: :unit },
compare_with: :quantity_value
Then where(quantity: Measurement.new(1, "kg")..Measurement.new(10, "kg")) would build
quantity_value BETWEEN 1.0 AND 10.0, and the remaining mapped columns would simply not
take part in that condition.
Things I’d want your input on:
-
Should
compare_withname the column (:quantity_value) or the value object’s reader (:value)? I lean toward the column, since the resulting condition compares a column and the column is what a caller writingwhere(...)thinks in terms of. Then again the reader is what the endpoints get projected through, so it’s arguable either way. -
Is
compare_with:the right name for it? -
If adding an option isn’t wanted, would a clearer error be acceptable instead — e.g. raising something that names the mapped attributes and says a
Rangeisn’t supported for this aggregate?
What works today without any core change
-
Keep
composed_ofand route the query around it: Register a custom attribute type for the value column, declare that column with the Attributes API, and alias the aggregate name to it. This seems like a “clever workaround” rather than a documented solution, and also depends on the internal implementation. I wrote in detail about it here. -
Query the underlying column directly:
Ingredient.where(quantity_value: 1..10)buildsquantity_value BETWEEN 1.0 AND 10.0. Nocomposed_ofinvolved, so you give up the value-object API for that condition.