1
我有一個集合非活動記錄模型,每個都有幾個外鍵。我想急切地加入協會,但我正在爲這種方法而努力。該代碼看起來是這樣的:渴望加載外活動記錄
型號:
class Tuple
attr_accessor :widget_id
attr_accessor :woogle_id
def self.all
# returns a bunch of tuples with ids
end
def widget
@widget ||= Widget.find(widget_id)
end
def woogle
@woogle ||= Woogle.find(woogle_id)
end
end
查看:
- Tuple.all.each do |tuple|
= render tuple.widget
= render tuple.woogle
任何在這種情況下如何渴望負載活動記錄外,避免N + 1個查詢的想法?
你用什麼來代替ActiveRecord?你的記錄如何存儲? – Substantial 2013-04-10 23:33:58
@gg_s關聯的記錄由ActiveRecord支持(只有Tuple不支持Active Record) – 2013-04-11 02:42:56