-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
activerecord: Use a simpler query condition for aggregates with one mapping #34963
activerecord: Use a simpler query condition for aggregates with one mapping #34963
Conversation
build(table.arel_attribute(field_attr), object) | ||
else | ||
if mapping.length == 1 | ||
column_name, aggr_attr = mapping.first |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer using the same variable names in both path.
column_name, aggr_attr = mapping.first | |
field_attr, aggregate_attr = mapping.first |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That causes "shadowing outer local variable" ruby warnings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh... I see...
…tected methods by default Co-Authored-By: dylanahsmith <dylan.smith@shopify.com>
…-field-query activerecord: Use a simpler query condition for aggregates with one mapping
Problem
When using an aggregate with one mapping, e.g.
The SQL query from
Customer.where(balance: (1..50).map { |amount| Money.new(amount) })
would be something likeinstead of the simpler query that we would get from not using
composed_of
(i.e. querying usingCustomer.where(balance: (1..50))
)https://stackoverflow.com/questions/782915/mysql-or-vs-in-performance shows this could have a performance impact, at least in older versions of mysql. Mostly I just want to avoid a database regression by using
composed_of
to introduce an abstraction.Solution
The code already had a special case for a single mapping, so I just separated this code path more to avoid building multiple conditions joined with OR.