如何在Rails中执行以下更新查询?我想在数据库中使用单个更新查询执行此操作。在伪代码中,我会写:
update answers set user_id = @user.id where answered_by=@user.email and user_id = 0
在SQL中它可能类似于:
UPDATE `answers` SET user_id = '123' WHERE answered_by = 'email@example.com' and user_id = 0
答案 0 :(得分:5)
Answer.update_all(["user_id = ?", @user.id], ["answered_by = ? and user_id == ?", @user.email, 0])