作者:mobiledu2502862777 | 来源:互联网 | 2023-09-14 19:12
Ivebeenpreventingupdatestocertainmodelsbyusingthisinthemodel:我一直在通过在模型中使用这个来阻止对某些模型的更新
I've been preventing updates to certain models by using this in the model:
我一直在通过在模型中使用这个来阻止对某些模型的更新:
def update
self.errors.add_to_base( "Cannot update a #{ self.to_s }" )
end
I'm now writing a plugin that delivers some extra functionality to the model, and I need to update one field in the model. If I weren't using a plugin I would do this directly in the model...
我现在正在编写一个插件来为模型提供一些额外的功能,我需要更新模型中的一个字段。如果我不使用插件,我会直接在模型中做这个……
def update
if self.changed == ['my_field']
super
else
self.errors.add_to_base( "Cannot update a #{ self.to_s }" )
end
end
I can't do the same from my plugin since I don't know if the update behaviour is the ActiveRecord default, or has been overridden to prevent updates. Is there another way to prevent record updates while allowing me to override for a specific field (and only in the instance where my plugin is applied to this model).
我不能对我的插件做同样的事情,因为我不知道更新行为是默认的ActiveRecord,还是已经被覆盖以防止更新。是否有另一种方法可以防止记录更新,同时允许我覆盖特定字段(并且只在我的插件应用到这个模型的实例中)。
4 个解决方案