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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Oct-26 08:46 UTC
Re: Validating model depending on controller action
On Oct 26, 9:36 am, Alfredo Bonilla <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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?" >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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #957145:> On Oct 26, 9:36am, Alfredo Bonilla <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 > > FredThanks 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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Oct-26 09:38 UTC
Re: Validating model depending on controller action
On Oct 26, 10:21 am, Alfredo Bonilla <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > 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. Fred> > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #957158:> On Oct 26, 10:21am, Alfredo Bonilla <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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. > > FredSorry 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) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Oct-26 12:25 UTC
Re: Re: Validating model depending on controller action
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Alfredo Bonilla
2010-Oct-26 13:59 UTC
Re: Re: Validating model depending on controller action
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.