Rails 3.x如何编写基于行值的更新? [英] Rails 3.x How to write update all based on row value?
问题描述
我希望在添加列后(在同一行中使用现有列值更新新字段)在迁移文件中执行以下SQL查询
I would like the following SQL query to be executed in a migration file after adding a column (updating the new field with existing column value from the same row)
UPDATE users SET last_login=updated_at;
在数据库上执行时,SQL语句正常工作,但是在Rails中,我使用ActiveRecord尝试了多种语法update_all方法但未成功
The SQL statements work properly when executed on the database, but in rails I tried multiple syntax using the ActiveRecord update_all method but without success
User.update_all("last_login=updated_at")
我收到以下错误
ActiveRecord::StatementInvalid: PGError: ERROR: current transaction is aborted, commands ignored until end of transaction block
: UPDATE "users" SET last_login=updated_at
显然我的语法中缺少某些内容,但无法弄清楚是什么。
谁能指出我正确的语法吗?
Obviously something is missing in my syntax, but can't figure out what. Can anyone point me to the right syntax?
致谢/ J。
推荐答案
语法确实正确,问题在于我必须回滚以前的交易。
Syntax is indeed correct, the issue was relying in the fact that I had to rollback the previous transaction.
User.update_all("last_login=updated_at")
此语句正常工作。
这篇关于Rails 3.x如何编写基于行值的更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!