# On Update, how can I validate that an attribute has not being already set?

**URL:** <https://www.ruby-forum.com/t/on-update-how-can-i-validate-that-an-attribute-has-not-being-already-set/130746>\
**Category:** Rails\
**Created:** [March 3, 2008, 12:02am UTC](https://www.ruby-forum.com/t/on-update-how-can-i-validate-that-an-attribute-has-not-being-already-set/130746 "2008-03-03T00:02:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [March 3, 2008, 12:02am UTC](https://www.ruby-forum.com/t/on-update-how-can-i-validate-that-an-attribute-has-not-being-already-set/130746/1 "2008-03-03T00:02:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [March 3, 2008, 12:43am UTC](https://www.ruby-forum.com/t/on-update-how-can-i-validate-that-an-attribute-has-not-being-already-set/130746/2 "2008-03-03T00:43:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [March 3, 2008, 4:38am UTC](https://www.ruby-forum.com/t/on-update-how-can-i-validate-that-an-attribute-has-not-being-already-set/130746/3 "2008-03-03T04:38:58Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

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