Re: validates_uniqueness_of question

6 views
Skip to first unread message

Gurdipe Dosanjh

unread,
Apr 18, 2012, 3:18:57 PM4/18/12
to rubyonra...@googlegroups.com
Hi All,

I am trying write a validation rule where I need to ensure that when a certain value is updated or created in a particular columns, a number of other columns columns are unique and that another column does not contain a certain value.

I have been able to use the validates_uniqueness_of :column_a (to be updated or inserted) with , :scope => [ :column_b ,  :column_c ,  :column_d ] .  This works well but now I also need to extend it so that a certain column is not equal to 0.

How can I extend the validation rule to also include that a check is carried out that this other column does not equal to 0

Kind Regards

Gurdipe

Dave Aronson

unread,
Apr 21, 2012, 3:29:18 PM4/21/12
to rubyonra...@googlegroups.com
On Wed, Apr 18, 2012 at 15:18, Gurdipe Dosanjh <rhomob...@gmail.com> wrote:

> How can I extend the validation rule to also include that a check is carried
> out that this other column does not equal to 0

You can use an exclusion clause. See:

http://guides.rubyonrails.org/active_record_validations_callbacks.html#exclusion

It uses an array, which in your case would just be [0]. There might
be a way to do just a single value, I don't know offhand, never had
to. Let us know if you find something like that....

-Dave

--
Dave Aronson:  Available Cleared Ruby on Rails Freelancer
(NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at
www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com

Reply all
Reply to author
Forward
0 new messages