# Selective validation? Is it possible?

**URL:** <https://discuss.rubyonrails.org/t/selective-validation-is-it-possible/3736>\
**Category:** rubyonrails-talk\
**Created:** [October 29, 2006, 10:55am UTC](https://discuss.rubyonrails.org/t/selective-validation-is-it-possible/3736 "2006-10-29T10:55:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Feurio1](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@Feurio1](https://discuss.rubyonrails.org/u/Feurio1)\
**Post date:** [October 29, 2006, 10:55am UTC](https://discuss.rubyonrails.org/t/selective-validation-is-it-possible/3736/1 "2006-10-29T10:55:35Z")

</div>

Hello Petr,

I think this can be done. Here provided an example were a validation runs only for certain customer types.

validates\_presence\_of :birthday, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:street, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:street\_number, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:zip\_code, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:city, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:bank\_name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:bank\_code, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:bank\_account, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:telefon\_number, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:message =\> "darf nicht leer sein.", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:if =\> Proc.new { | customer | customer.customer\_type.name == "partner" or customer.customer\_type.name == "customer" }

The important part for you is the :if part. Only for object that pass the test in the Proc the validation is used.

There is a "has\_parent?" method for act\_as\_tree objects. I think this could be used for your problem...

Feurio

---

<div class="post-metadata">

**Author:** ![Jean-Francois1](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@Jean-Francois1](https://discuss.rubyonrails.org/u/Jean-Francois1)\
**Post date:** [October 29, 2006, 11:13am UTC](https://discuss.rubyonrails.org/t/selective-validation-is-it-possible/3736/2 "2006-10-29T11:13:03Z")

</div>

Hello Feurio,

> There is a "has\_parent?" method for act\_as\_tree objects. I think this could be used for your problem...

acts\_as\_tree defines a belongs\_to association named 'parent', so you can benefit from a #has\_parent? method. However this method is deprecated and you should use #parent (that is, the method with the name of the association), if there is no associated record, it returns nil then in a condition it will be false so you can use it.

Note that if you have a has\_many :foos association, #has\_foos? is also deprecated.

&nbsp;&nbsp;&nbsp;-- Jean-François.
