Updating all records in single transaction
Right now I开发者_如何学C am using Rails AR methods to update a bunch of attributes in a table.
RaceWeek.current_week.each { |r| r.update_attributes(:games_won => 0, :games_lost => 0) }
I'd prefer to do this more efficiently in a single transaction. How can this be done?
Use update_all
:
<model/association>.update_all("games_won = 0, games_lost = 0" )
Ps. Use some form of the above, because I do not know what the relation of RaceWeek
to current_week
is.
Also see some examples
精彩评论