shane
2008-Mar-02 23:02 UTC
On Update, how can I validate that an attribute has not being already set?
Hi I have a boolean attribute on a model ... user_approved ... that I wish to apply some validation to upon update ... The business rule is "You cannot update this model to user_approved if it already has been" .... but I am not sure if I can do this using the normal rails validations? For example, using update_attributes, how do I tell that they have requested to update this field as opposed to updating another? Anyone have any elegant solutions? Thanks Shane --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Bush
2008-Mar-02 23:43 UTC
Re: On Update, how can I validate that an attribute has not
shane wrote:> The business rule is "You cannot update this model to user_approved if > it already has been" .... but I am not sure if I can do this using the > normal rails validations?You probably want to add this to the model: attr_protected :user_approved This will prevent the attribute from being set by #update_attributes and so will protect you from someone sending their own form content to your update actions and sneaking in a change. You can then override the #user_approved= method in the model to do the validation. When attributes are set, their #attribute= methods are called to set the values. You can override these as you like to validate the input and check the current value before making any changes. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar)
2008-Mar-03 03:37 UTC
Re: On Update, how can I validate that an attribute has not being already set?
The business rule is fail. If it''s already set to true, why does updating it true matter? On Mon, Mar 3, 2008 at 9:32 AM, shane <shane.mingins-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi > > I have a boolean attribute on a model ... user_approved ... that I > wish to apply some validation to upon update ... > > The business rule is "You cannot update this model to user_approved if > it already has been" .... but I am not sure if I can do this using the > normal rails validations? > > For example, using update_attributes, how do I tell that they have > requested to update this field as opposed to updating another? > > Anyone have any elegant solutions? > > Thanks > Shane > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---