# Validates\_presence\_of question

**URL:** <https://www.ruby-forum.com/t/validates-presence-of-question/67751>\
**Category:** Rails\
**Created:** [September 6, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/validates-presence-of-question/67751 "2006-09-06T16:12:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@dimon](https://www.ruby-forum.com/u/dimon)\
**Post date:** [September 6, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/validates-presence-of-question/67751/1 "2006-09-06T16:12:37Z")

</div>

Can I use validates\_presence\_of somehow if I want to check that at least  
one of 2 fields is not empty?

Thanks,  
Dmitry

---

<div class="post-metadata">

**Author:** ![dimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@dimon](https://www.ruby-forum.com/u/dimon)\
**Post date:** [September 6, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/validates-presence-of-question/67751/2 "2006-09-06T16:44:13Z")

</div>

I’m still new to Rails, but I think you need to add a ‘validate’ method  
to your controller and perform the check in there instead.

Off the top of my head, something like:

protected  
def validate  
errors.add(“Either tweedledum or tweedledee need to be entered.”)  
unless tweedledum != nil or tweedledee != nil  
end

I can’t see a way of using validates\_presence\_of for this.

Regards,

Chris.

---

<div class="post-metadata">

**Author:** ![dimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@dimon](https://www.ruby-forum.com/u/dimon)\
**Post date:** [September 7, 2006, 6:33am UTC](https://www.ruby-forum.com/t/validates-presence-of-question/67751/3 "2006-09-07T06:33:44Z")

</div>

I think Chris meant to tell you to add the validate method to the model.  
This will then be called in the normal validation routine.

Mark

On 9/7/06, Dmitry H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > errors.add(“Either tweedledum or tweedledee need to be entered.”)  
> > And is it possible to embed this ‘validate’ method into the model; say,  
> > into the before\_save filter or something like that?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Mark Van H.  
removed\_email\_address@domain.invalid  
[http://lotswholetime.com](http://lotswholetime.com)

---

<div class="post-metadata">

**Author:** ![dimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@dimon](https://www.ruby-forum.com/u/dimon)\
**Post date:** [September 7, 2006, 6:03am UTC](https://www.ruby-forum.com/t/validates-presence-of-question/67751/4 "2006-09-07T06:03:18Z")

</div>

Chris D. wrote:

> I’m still new to Rails, but I think you need to add a ‘validate’ method  
> to your controller and perform the check in there instead.
> 
> Off the top of my head, something like:
> 
> protected  
> def validate  
> errors.add(“Either tweedledum or tweedledee need to be entered.”)  
> unless tweedledum != nil or tweedledee != nil  
> end
> 
> I can’t see a way of using validates\_presence\_of for this.
> 
> Regards,
> 
> Chris.

Thanks, Chris!  
And is it possible to embed this ‘validate’ method into the model; say,  
into the before\_save filter or something like that?

---

<div class="post-metadata">

**Author:** ![dimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@dimon](https://www.ruby-forum.com/u/dimon)\
**Post date:** [September 7, 2006, 6:41am UTC](https://www.ruby-forum.com/t/validates-presence-of-question/67751/5 "2006-09-07T06:41:45Z")

</div>

> Thanks, Chris!  
> And is it possible to embed this ‘validate’ method into the model; say,  
> into the before\_save filter or something like that?

If you have a method called “validate” on your model, this is used  
automatically by the valid? method. No need to do anything else.

Max

---

<div class="post-metadata">

**Author:** ![dimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@dimon](https://www.ruby-forum.com/u/dimon)\
**Post date:** [September 7, 2006, 7:07am UTC](https://www.ruby-forum.com/t/validates-presence-of-question/67751/6 "2006-09-07T07:07:48Z")

</div>

Hi !

2006/9/7, Max M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> If you have a method called “validate” on your model, this is used  
> automatically by the valid? method. No need to do anything else.

The only problem with that is if you have subclasses that override the  
validate method. Unless the subclass calls the superclass method,  
your validations won’t work as expected.

class Order \< AR::Base  
def validate  
# this will never be called  
end  
end

class RushOrder \< Order  
def validate  
self.errors.add(:status, ‘invalid’) if self.status.blank?  
end  
end

If you do this instead:

class Order \< AR::Base  
before\_validation :check\_base\_order  
def check\_base\_order  
# this will always be called  
end  
end

class RushOrder \< Order  
before\_validation :check\_rush\_order  
def check\_rush\_order  
# this will ALSO always be called  
end  
end

## Hope that helps !

FranÃ§ois Beausoleil  
[http://blog.teksol.info/](http://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![dimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@dimon](https://www.ruby-forum.com/u/dimon)\
**Post date:** [September 11, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/validates-presence-of-question/67751/7 "2006-09-11T20:36:53Z")

</div>

Yes, I meant model, but wrote controller. Also it should be  
@tweedledum etc. The ‘unless’ is a continuation of the ‘errors.add’  
line, but it’s come out with bad formatting.

Regards,

Chris.
