如何使用条件更新数据库表中的值?

时间:2013-04-27 08:57:55

标签: mysql ruby-on-rails

我需要在表where id == question_id中更新'状态'字段。我尝试了这个,但它不起作用:

Questions.update_all("status = status_id.to_i", 
               ["id = question_id"])

我有这个错误:

Mysql2::Error: Unknown column 'question_id' in 'where clause': UPDATE `questions` SET status = status_id.to_i WHERE (id = question_id)

1 个答案:

答案 0 :(得分:1)

像这样的Smth?

Questions.where(id: question_id).update_all(status: status_id.to_i)