# Requiring one field or another

**URL:** <https://www.ruby-forum.com/t/requiring-one-field-or-another/89060>\
**Category:** Rails\
**Created:** [March 16, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/requiring-one-field-or-another/89060 "2007-03-16T21:14:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [March 16, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/requiring-one-field-or-another/89060/1 "2007-03-16T21:14:45Z")

</div>

I’m fairly new to Rails so I’m having some trouble with what is probably  
an easy task. Any assistance would be appreciated.

I have two fields, I only have to have one of them filled in…

At the moment I’m working on some variation of

validates\_presence\_of :foo, :if =\> “bar.empty?”

Verbatim, that will of course not work…

Suggestions?

---

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [March 16, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/requiring-one-field-or-another/89060/2 "2007-03-16T21:33:26Z")

</div>

On 16-Mar-07, at 4:14 PM, Tom von S. wrote:

> Verbatim, that will of course not work…
> 
> Suggestions?

Tom, :if takes a ‘block’

validates\_presence\_of :foo, :if =\> Proc.new { | your\_model\_instance  
| your\_model\_instance.bar.empty? }

as you can see, the block is passed the current instance, which you  
can examine - returning tru means the validation will take place.

cheers,  
Jodi

---

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [March 16, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/requiring-one-field-or-another/89060/3 "2007-03-16T23:11:59Z")

</div>

Jodi S. wrote:

> Tom, :if takes a ‘block’
> 
> validates\_presence\_of :foo, :if =\> Proc.new { | your\_model\_instance  
> | your\_model\_instance.bar.empty? }
> 
> as you can see, the block is passed the current instance, which you  
> can examine - returning tru means the validation will take place.

Ah, much like the example in the docs (which I somehow didn’t “get”).

Thank you,  
Tom
