我正在使用以下方法更新活动记录表:
def sort
params[:piece].each_with_index do |id, index|
current_user.lineup.piece_lineups.update_all({position: index+1}, {id: id})
end
render nothing: true
end
一切看起来都在工作,只是它实际上并没有将新的排序顺序保存到数据库中。查看输出,它说它正在正确更新所有内容。这是sql查询:
Started POST "/lineups/sort" for 127.0.0.1 at 2012-06-26 08:07:24 -0700
Processing by LineupsController#sort as */*
Parameters: {"piece"=>["1", "4", "2", "3", "7"]}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1
Lineup Load (0.2ms) SELECT `lineups`.* FROM `lineups` WHERE `lineups`.`user_id` = 3 LIMIT 1
SQL (0.2ms) UPDATE `piece_lineups` SET `position` = 1 WHERE `piece_lineups`.`lineup_id` = 4 AND `piece_lineups`.`id` = 1
SQL (0.2ms) UPDATE `piece_lineups` SET `position` = 2 WHERE `piece_lineups`.`lineup_id` = 4 AND `piece_lineups`.`id` = 4
SQL (0.2ms) UPDATE `piece_lineups` SET `position` = 3 WHERE `piece_lineups`.`lineup_id` = 4 AND `piece_lineups`.`id` = 2
SQL (0.2ms) UPDATE `piece_lineups` SET `position` = 4 WHERE `piece_lineups`.`lineup_id` = 4 AND `piece_lineups`.`id` = 3
SQL (0.1ms) UPDATE `piece_lineups` SET `position` = 5 WHERE `piece_lineups`.`lineup_id` = 4 AND `piece_lineups`.`id` = 7
Rendered text template (0.0ms)
Completed 200 OK in 8ms (Views: 0.6ms | ActiveRecord: 1.4ms)
似乎一切都应该正常工作,但事实并非如此。我错过了什么?