Using a boolean attribute as a conditional on an update action on a model in Rails 3 -
imagine have boolean , string attribute on model data this:
model.locked => true model.status => "available"
when update action invoked, want use boolean attribute control whether string attribute gets modified update, other attributes in update should saved regardless.
so if
model.locked => true
and try
model.status = "sold"
then
model.status => "available"
currently have model this...
before_update :dont_update_locked_item, :if => :item_locked? def item_locked? self.locked end def dont_update_locked_item #some rails magic goes here end
how prevent single attribute being updated?
i know answer going hurt, me out. it's late, i'm tired , i'm fresh out of talent. ;-)
hope doesn't hurt :)
class item < activerecord::base validate :cannot_update_if_locked, :on => :update def item_locked? self.locked end def cannot_update_if_locked errors.add(:item_id, "is locked") if item_locked? end end
Comments
Post a Comment