gpt4 book ai didi

ruby-on-rails - 在 Rails 中递增数据库值

转载 作者:行者123 更新时间:2023-12-04 03:37:18 25 4
gpt4 key购买 nike

在 Rails 中,我想通过递增来更新一些数据库值。
说,我在表中有值 30,我想添加 0.5。

这是我尝试完成它的方法:

Record.where(:status => "somestatus").first.value.to_f += 0.5

RoR 提示说“未定义的方法 `to_f='”。
我知道我可以先获取值,最后计算结果和 update_attributes。但最有效的方法是什么?

最佳答案

希望场是 Float 类型,以下将根据您的需要更新值。

object = Record.where(:status => "somestatus").first
object.increment!(:value, 0.5)

Check here有关 的文档增量! 方法。

关于ruby-on-rails - 在 Rails 中递增数据库值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15561095/

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