gpt4 book ai didi

ruby-on-rails - Ruby on Rails 事件记录 : update a field by another field in the same record using update_all

转载 作者:行者123 更新时间:2023-12-04 06:21:46 24 4
gpt4 key购买 nike

如何将 update_all 用于以下目的:

UPDATE  table_name set field1=field2 * 2 where id = 1

最佳答案

在 Rails4 中,你可以做到

Model.where(id: 1).update_all("field = field2 * 2")

在 Rails 的早期版本中,您可以将条件传递给 update_all,如下所示

Model.update_all("field = field2 * 2", {:id => 1})

注意:

update_all 触发 Active Record 回调或验证

关于ruby-on-rails - Ruby on Rails 事件记录 : update a field by another field in the same record using update_all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30116094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com