gpt4 book ai didi

ruby activerecord 简单的一个 : call attribute by variable

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:39 24 4
gpt4 key购买 nike

我想更新由 activerecord 模型中的变量定义的属性。

这是我的方法

def increment_attribute(attr_name)
self.attribute(attr_name).increment
end

这不起作用,因为属性是一个私有(private)的 activerecord 方法

我应该怎么做?谢谢!

最佳答案

def increment_attribute(name)
self.send(name).increment
end

关于ruby activerecord 简单的一个 : call attribute by variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9955120/

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