Rails 3.x How to write update all based on row value?

北城以北 提交于 2019-12-11 04:59:54

问题


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;

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")

I get the following error

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?

Regards/J.


回答1:


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")

This statement works properly.



来源:https://stackoverflow.com/questions/10347618/rails-3-x-how-to-write-update-all-based-on-row-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!