【发布时间】:2014-07-20 10:11:15
【问题描述】:
我想覆盖store_accessor 的getter。可以找到here。代码在这里:
# File activerecord/lib/active_record/store.rb, line 74
def store_accessor(store_attribute, *keys)
keys = keys.flatten
_store_accessors_module.module_eval do
keys.each do |key|
define_method("#{key}=") do |value|
write_store_attribute(store_attribute, key, value)
end
define_method(key) do
read_store_attribute(store_attribute, key)
end
end
end
self.stored_attributes[store_attribute] ||= []
self.stored_attributes[store_attribute] |= keys
end
我实现了我想要的功能,但是如果我还要覆盖 setter,有一个我不清楚的方法,它在 write_store_attribute(...) 方法中(找到 here)。
代码在这里:
# File activerecord/lib/active_record/store.rb, line 108
def write_store_attribute(store_attribute, key, value)
attribute = initialize_store_attribute(store_attribute)
if value != attribute[key]
send :"#{store_attribute}_will_change!"
attribute[key] = value
end
end
我不明白的方法是"#{whatever_store_att}_will_change!"。
如果我要覆盖 setter,我会使用 update_attributes 或 update_column。这种赋值attribute[key]=value的方法是否实际上修改了数据库中的字段,使其等效于update_attributes?
【问题讨论】:
标签: ruby-on-rails ruby postgresql activerecord hstore