我想用after_save中的数组更新一个字段。
同时,有不同的其他after_saves执行,忽略这一点。
我尝试使用update_column,但是它没有序列化数组,所以我需要让它与update_attribute或update_attributes一起工作。
How can I update array to a single field without performing callbacks发布于 2014-09-09 19:21:00
如果您想跳过回调和验证,可以使用列(名称、值)及其配套的列(属性)方法。在类范围内有所有(更新)。
或者,您可以使用JSON (而不是序列化数组),例如:
class Bar < ActiveRecord::Base
def foo= array
write_attribute :foo, array.to_json
end
def foo
JSON.parse(read_attribute :foo)
end
end在其他地方:
Bar.first.update_column :foo, [1,2,3].to_json至少,当您处理序列化ActiveRecord::Store时,您可能会很感兴趣。
https://stackoverflow.com/questions/25751154
复制相似问题