vendredi 8 avril 2016

Rails: query execution even with eager load

In Rails 3, given an Order that has many OrderRows,

if I call the sum function, I get an unwanted query executed

Order.eager_load(:order_rows).sum{|x| x.unit_taxed_cents}
OrderRow Load (0.6ms)  SELECT "order_rows".* FROM "order_rows" WHERE "order_rows"."order_id" = XXXX
=> 17900

instead, if I use, for example, a map, the additional query is not executed:

a.order_rows.inject(0){|sum, r| sum + r.unit_taxed_cents }
=> 17900

By definition (http://ift.tt/1UNBale) the 2 functions should be the same thing

Why doensn't it behave like expected?

Thanks

Aucun commentaire:

Enregistrer un commentaire