# Validates Uniqueness unless blank?

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-unless-blank/113200>\
**Category:** Rails\
**Created:** [August 28, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-unless-blank/113200 "2007-08-28T17:54:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 28, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-unless-blank/113200/1 "2007-08-28T17:54:42Z")

</div>

Hopefully this is an easy question…

I want to validate the uniqueness of a field, unless that field is  
blank… ie they didn’t put anything in for it…

Any ideas? Thanks guys.

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 28, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-unless-blank/113200/2 "2007-08-28T18:10:46Z")

</div>

On 8/28/07, sw0rdfish [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hopefully this is an easy question…
> 
> I want to validate the uniqueness of a field, unless that field is  
> blank… ie they didn’t put anything in for it…

validates\_uniqueness\_of :field, :if Proc.new {|model|  
!model.field.blank?}

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 28, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-unless-blank/113200/3 "2007-08-28T19:45:11Z")

</div>

what does Proc.new do? I get the rest, little fuzzy on that one.

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 28, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-unless-blank/113200/4 "2007-08-28T20:15:22Z")

</div>

Awesome thanks!

I figured out that the =\> was missing… worked perfectly! Thanks  
Adam

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 28, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-unless-blank/113200/5 "2007-08-28T20:08:20Z")

</div>

On 8/28/07, sw0rdfish [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> what does Proc.new do? I get the rest, little fuzzy on that one.
> 
> > validates\_uniqueness\_of :field, :if Proc.new {|model| !model.field.blank?}

there’s actually a typo there, it should be:

validates\_uniqueness\_of :field, :if =\> Proc.new {|model|  
!model.field.blank?}

Using Proc.new is just a short hand of using the following:

validates\_uniqueness\_of :field, :if =\> :some\_field\_is\_not\_blank?

def some\_field\_is\_not\_blank?  
!self.some\_field.blank?  
end

Here’s some information about Proc from the Pickaxe book:

Proc objects are blocks of code that have been bound to a set of local  
variables. Once  
bound, the code may be called in different contexts and still access  
those variables

def gen\_times(factor)  
return Proc.new {|n| n\*factor }  
end

times3 = gen\_times(3)  
times5 = gen\_times(5)  
times3.call(12) —\> 36  
times5.call(5) —\> 25  
times3.call(times5.call(4)) —\> 60
