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

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.

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 [email protected] wrote:

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 B.

Feel free to add me to MSN and/or GTalk as this email.