# How would I construct this validation rule?

**URL:** <https://www.ruby-forum.com/t/how-would-i-construct-this-validation-rule/132211>\
**Category:** Rails\
**Created:** [March 17, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/how-would-i-construct-this-validation-rule/132211 "2008-03-17T22:54:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 17, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/how-would-i-construct-this-validation-rule/132211/1 "2008-03-17T22:54:40Z")

</div>

Hi,

I have a column, “store\_id” in my users table that could be blank, but  
otherwise it must be numeric. If someone does decide to enter a  
number, I want to ensure that number is unique relative to other  
store\_id’s with non-null values. How, if possible, would I construct  
such a validation rule in my user model?

Thanks, - Dave

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 18, 2008, 12:15am UTC](https://www.ruby-forum.com/t/how-would-i-construct-this-validation-rule/132211/2 "2008-03-18T00:15:27Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I have a column, “store\_id” in my users table that could be blank, but  
> otherwise it must be numeric. If someone does decide to enter a  
> number, I want to ensure that number is unique relative to other  
> store\_id’s with non-null values. How, if possible, would I construct  
> such a validation rule in my user model?

validates\_uniqueness\_of :store\_id, :allow\_nil =\> true  
validates\_numericality\_of :store\_id, :allow\_nil =\> true

And avoid use of #update\_attribute (as this method avoids validations by  
default).
