ruby on rails - How can I disallow updates except for on one field? -
i've been preventing updates models using in model:
def update self.errors.add_to_base( "cannot update #{ self.to_s }" ) end
i'm writing plugin delivers functionality model, , need update 1 field in model. if weren't using plugin directly in model...
def update if self.changed == ['my_field'] super else self.errors.add_to_base( "cannot update #{ self.to_s }" ) end end
i can't same plugin since don't know if update behaviour activerecord default, or has been overridden prevent updates. there way prevent record updates while allowing me override specific field (and in instance plugin applied model).
first, should using before_update callback sort of thing rather overriding update. second, can store updatable attributes on model, , update them plugin. wrote in browser, wrong.
attr_accessor :updatable_attributes before_update :prevent_update private def prevent_update return true if self.changed == self.updatable_attributes self.errors.add_to_base "cannot update #{ self.to_s }" false end end
Comments
Post a Comment