How would I construct this validation rule?

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

[email protected] 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).