How do we know if a query is cache or retrieved from database?
Posted
by Hadi
on Stack Overflow
See other posts from Stack Overflow
or by Hadi
Published on 2010-06-05T03:05:35Z
Indexed on
2010/06/07
6:02 UTC
Read the original article
Hit count: 299
ruby-on-rails
|activerecord
For example:
class Product
has_many :sales_orders
def total_items_deliverable
self.sales_orders.each { |so| #sum the total }
#give back the value
end
end
class SalesOrder
def self.deliverable
# return array of sales_orders that are deliverable to customer
end
end
SalesOrder.deliverable
#give all sales_orders that are deliverable to customerpa = Product.find(1)
pa.sales_orders.deliverable
#give all sales_orders whose product_id is 1 and deliverable to customerpa.total_so_deliverable
The very point that i'm going to ask is: how many times SalesOrder.deliverable is actually computed, from point 1, 3, and 4, They are computed 3 times that means 3 times access to database
so having total_so_deliverable is promoting a fat model, but more database access. Alternatively (in view) i could iterate while displaying the content, so i ends up only accessing the database 2 times instead of 3 times.
Any win win solution / best practice to this kind of problem ?
© Stack Overflow or respective owner