我有這樣的has_many關係:的has_many關係重載父
class Event < ActiveRecord::Base
has_many :event_rows
...
end
class EventRow < ActiveRecord::Base
belongs_to :event
...
end
當我運行下面的代碼片段,它告訴我36個查詢:
@events = Event.where("date = ?", @date).all
@events.each do |e|
first = e.event_rows.first
first.event
end
當我運行這個片段中,我只得到19查詢
@events = Event.where("date = ?", @date).all
@events.each do |e|
first = e.event_rows.first
first.event = e # forcing the parent
first.event
end
我做錯了什麼,或者rails每次運行一個查詢EventRow都會詢問它s事件?
在迴應評論:查詢是不標記爲CACHED:
SQL (0.7ms)[0m SELECT "event_rows"."id" AS t0_r0, "event_rows"."event_id" ... WHERE "event_rows"."event_id" = 1 LIMIT 1
Event Load (0.6ms)[0m [1mSELECT "events".* FROM "events" WHERE "events"."id" = 1 LIMIT 1[0m
SQL (3.2ms)[0m SELECT "event_rows"."id" AS t0_r0, "event_rows"."event_id" ... WHERE "event_rows"."event_id" = 2 LIMIT 1
Event Load (0.5ms)[0m [1mSELECT "events".* FROM "events" WHERE "events"."id" = 2 LIMIT 1[0m
我想這36個查詢中的大部分都被標記爲緩存在日誌中,不是嗎? –
我現在試過了,它們沒有標記爲緩存。 – carlosvini
啊,這是在發展?在開發中默認禁用模型緩存,這可能是原因 –