一個Order
有很多孩子Items
。 Items
有很多孩子Charges
。在def display
類型Controller方法及其相關視圖中,我期望顯示:1)order
及其子items
的所有屬性和2)amount
屬性從Charges
以各種用戶友好組合的總和。例如,當您只需要使用其中一個屬性時,是否優化內存以加載子對象?
<%= @order.id %>
<%= @order.name %>
<% @order.items.each do |item| %>
<%= item.id %>
<%= item.name %>
<% end %>
@total_item_charges
# below 2 should sum into @total_item_charges above
@total_item_type_a_charges
@total_item_type_b_charges
因爲我需要所有的items
屬性後,我想我知道渴望載荷,但鑑於我才真正需要的charges
(1至查詢)1個屬性,應該我渴望載荷爲好?
解決方法A
使用includes
躍躍欲試負載孩子items
以及孫子charges
,以便需要的所有信息被加載一次,使用
# CONTROLLER
@order = Order.includes(items: [:charges]).where(id: params[:order_id]).first
# items/charges already loaded into memory
@order.items.each do |i|
i.charges.each do |c|
if c.type == "type_a"
@total_item_type_a_charges += c.amount
else
@total_item_type_b_charges += c.amount
end
end
end
@total_item_charges = @total_item_type_a_charges + @total_item_type_b_charges
B方法
仍渴望加載的孩子items
,但不是grandchi ldren charges
,而不是做where
查詢需要
# CONTROLLER
@order = Order.includes(:items).includes(:shipments).where(id: params[:order_id]).first
@order.items.each do |i|
@total_item_type_a_charges += i.charges.where(type:"type_a").pluck(:amount).inject(:+)
@total_item_type_b_charges += i.charges.where(type:"type_b").pluck(:amount).inject(:+)
end
@total_item_charges = @total_item_type_a_charges + @total_item_type_b_charges
在你的答案時......
- 共標誌,如果我使用
includes
完全錯誤的一切(一飲而盡,希望情況並非如此) - 如果在內存使用和速度之間進行權衡(即一種方法更快,但使用更多內存,而另一種更慢但使用更少,請幫我標記,因爲這會有所幫助)
- 如果t他的回答是,要麼是罰款,要麼取決於細微的情況,我問的是因爲我想知道這裏是否有經驗法則
- 想知道如何在控制檯中測試這個嗎?我可以看到加載每個查詢的毫秒數,但不是內存使用率