gpt4 book ai didi

ruby-on-rails-3 - 导轨 3 : difference between write_attribute and update_attribute

转载 作者:行者123 更新时间:2023-12-03 22:25:10 30 4
gpt4 key购买 nike

直到今天我才知道 write_attribute ...

它看起来像 update_attribute,虽然没有调用验证仍然调用 :before_save 回调,而 write_attribute 没有。

这是这两种方法的区别吗?

最佳答案

update_attribute实际上对数据库进行了物理调用。你会得到一个 UPDATE 的完整执行陈述。就像 update_attributes但仅适用于单列。

虽然 write_attribute将要分配给基于 AR 的列的模型的属性写入。如果您要覆盖基于数据库的属性。

def first_name=(val)
write_attribute :first_name, val
end
# some_model.first_name => 'whatever val is'

def first_name=(val)
@first_name = val
end
# some_model.first_name => nil

我还没看 write_attribute广泛,但我收集基于 Activerecord 的模型处理基于 db 的列的分配与您运行的 Mill 访问器略有不同。

关于ruby-on-rails-3 - 导轨 3 : difference between write_attribute and update_attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6158301/

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