Validating model depending on controller action

Hi,

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?”

Thanks in advance

On Oct 26, 9:36am, Alfredo B. [email protected] wrote:

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

Fred

Frederick C. wrote in post #957145:

On Oct 26, 9:36am, Alfredo B. [email protected] wrote:

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

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?

Frederick C. wrote in post #957158:

On Oct 26, 10:21am, Alfredo B. [email protected] wrote:

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.

Fred

Sorry about this, but… if I raise an exception… the update would be
cancelled in both cases (coming from regular update or from this
“reopen” action). I have to allow the second way (coming from reopen)

I do something similar, but you only need to remove it from mass
assignment

controller

@thing = Thing.new(params[:thing])
@thing.value = “enchilada”

model

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

Thanks to everybody for your answers.

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.

On Oct 26, 10:21am, Alfredo B. [email protected] wrote:

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.

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs