How do we know if a query is cache or retrieved from database?
- by Hadi
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 customer
pa = Product.find(1)
pa.sales_orders.deliverable #give all sales_orders whose product_id is 1 and deliverable to customer
pa.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 ?