Hi! Newbie question so pleae bear with me.
What is the difference between model.sum(&:column) and model.sum(:column)?
I ask because I saw this code
In a redmine (rails) plugin.
Hi! Newbie question so pleae bear with me.
What is the difference between model.sum(&:column) and model.sum(:column)?
I ask because I saw this code
In a redmine (rails) plugin.
First one is a Rails method that will be converted into a SQL query and return the sum. Second one is a Ruby method that cause Rails to load every order_item in memory, pass them to Ruby to do the sum.
Take a look at the query log and the difference will be obvious:
> Order.where(paid_at: Date.current..).sum(:item_total)
# DEBUG -- : Order Sum SELECT SUM("orders"."item_total") FROM "orders" WHERE "orders"."paid_at" >= $1 [["paid_at", "2023-07-12"]]
> Order.where(paid_at: Date.current..).sum(&:item_total)
# DEBUG -- : Order Load SELECT "orders".* FROM "orders" WHERE "orders"."paid_at" >= $1 [["paid_at", "2023-07-12"]]
TL:DR: If you have not accessed ib_service_order_items
before in your code, (:total_usts)
will be faster (no need to load every item in memory before summing). If you have accessed ib_service_order_items
before in your code, (&:total_usts)
will be faster (not need for the extra query, just use the objects in memory.
That was quite clear! Thanks!