2011-05-15 25 views
0

現在我正在使用Rails AR方法更新表中的一組屬性。在單個事務中更新所有記錄

RaceWeek.current_week.each { |r| r.update_attributes(:games_won => 0, :games_lost => 0) } 

我寧願在單筆交易中更有效地做到這一點。如何才能做到這一點?

+0

current_week是返回落於指定時間段的所有RaceWeek記錄named_scope。 – keruilin 2011-05-15 04:13:30

回答

0

使用update_all

<model/association>.update_all("games_won = 0, games_lost = 0") 

詩篇。使用上面的某種形式,因爲我不知道RaceWeekcurrent_week是什麼關係。

另見some examples