I have a custom validation in my model, reviewing the content of some
field. This field indicates that the record is "closed", so it can not
be modified.
The only way to "reopen" the record is by executing an specific model
action: reopen.
So my question is: "how can I avoid to validate this field JUST for the
controller reopen action?"
Models (quite rightly) have no concept of controller actions. If you
want to do this then you have to change some of the model's state (eg
set an instance variable) when reopen is called and check that in your
validation.
A validation doesn't feel quite right to me. While it sounds like you
can probably do what you want it sounds like you are stretching the
semantics slightly: it's not that a particular combination of
attributes that are invalid, it's a set of transitions that are
invalid. I'd be more inclined to write a before_validation callback
Models (quite rightly) have no concept of controller actions. If you
want to do this then you have to change some of the model's state (eg
set an instance variable) when reopen is called and check that in your
validation.
A validation doesn't feel quite right to me. While it sounds like you
can probably do what you want it sounds like you are stretching the
semantics slightly: it's not that a particular combination of
attributes that are invalid, it's a set of transitions that are
invalid. I'd be more inclined to write a before_validation callback
Fred
Thanks for your answer. I would use the instance variable, but if my
code in the model looks like:
validate :minutes_editable
...
def minutes_editable
if minutes_closed_was
errors.add(:minutes_closed, "specifies that the board is closed.
To reopen, use contextual menu action.")
end
end
where minutes_closed is the boolean field that just can be passed from
true to false by executing the "reopen" action.
How would you use the before_validation callback?
Thanks for your answer. I would use the instance variable, but if my
code in the model looks like:
validate :minutes_editable
...
def minutes_editable
if minutes_closed_was
errors.add(:minutes_closed, "specifies that the board is closed.
To reopen, use contextual menu action.")
end
end
where minutes_closed is the boolean field that just can be passed from
true to false by executing the "reopen" action.
Sorry not sure what you're asking now
How would you use the before_validation callback?
It would be pretty much the same except that instead of adding an
error I'd raise some appropriate exception.
attr_accessible :name, :description <===== value cant be mass_assigned
since is done programatically there is no need to validate it
even better create a class method in the model that raises an exception if it fails and call instead of the save method, in it set the value you want : )
def selft.supersave!
do the stuff
or raise Exception.new(“OMG LOL”)
end
I have to say that finally I resorted to some "work around" solution.
From the "reopen" method in the controller I modify another model vble
adding special chars. In the model validation method, I search for those
chars in the vble and if they are there, remove them and do not perform
the regular validation over the boolean field.