我需要一个示例在rails 3.0中运行更新查询

时间:2011-02-03 01:57:21

标签: ruby-on-rails-3

如何在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

1 个答案:

答案 0 :(得分:5)

Answer.update_all(["user_id = ?", @user.id], ["answered_by = ? and user_id == ?", @user.email, 0])