# Selective validation? Is it possible?

**URL:** <https://www.ruby-forum.com/t/selective-validation-is-it-possible/74032>\
**Category:** Rails\
**Created:** [October 29, 2006, 9:54am UTC](https://www.ruby-forum.com/t/selective-validation-is-it-possible/74032 "2006-10-29T09:54:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 29, 2006, 9:54am UTC](https://www.ruby-forum.com/t/selective-validation-is-it-possible/74032/1 "2006-10-29T09:54:37Z")

</div>

I have a Product model and products table. Im using acts\_as\_tree  
structure. I would like active record validation to be done on children  
only. Anyway to do this?

Petr

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 29, 2006, 11:34am UTC](https://www.ruby-forum.com/t/selective-validation-is-it-possible/74032/2 "2006-10-29T11:34:40Z")

</div>

Hello Petr,

> I have a Product model and products table. Im using acts\_as\_tree  
> structure. I would like active record validation to be done on children  
> only. Anyway to do this?

The root product is the product who has no parent.  
So in your validation, you could pass an :if option and a condition  
in the block :

:if =\> Proc.new { |r| r.parent }

Example :  
validates\_presence\_of :name, :if =\> Proc.new { |r| r.parent }

– Jean-FranÃ§ois.

–  
Ã? la renverse.

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 29, 2006, 11:56am UTC](https://www.ruby-forum.com/t/selective-validation-is-it-possible/74032/3 "2006-10-29T11:56:41Z")

</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,  
:street,  
:street\_number,  
:zip\_code,  
:city,  
:bank\_name,  
:bank\_code,  
:bank\_account,  
:telefon\_number,  
:message =\> “darf nicht leer sein.”,  
: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:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 29, 2006, 12:02pm UTC](https://www.ruby-forum.com/t/selective-validation-is-it-possible/74032/4 "2006-10-29T12:02:58Z")

</div>

Hey, thanks. I have one more question, could you describe to me what  
that does in detail? I know Proc objects from Ruby, I just cant imagine  
how it works in this case.

Petr

Jean-FranÃ§ois wrote:

> Hello Petr,
> 
> > I have a Product model and products table. Im using acts\_as\_tree  
> > structure. I would like active record validation to be done on children  
> > only. Anyway to do this?
> 
> The root product is the product who has no parent.  
> So in your validation, you could pass an :if option and a condition  
> in the block :
> 
> :if =\> Proc.new { |r| r.parent }
> 
> Example :  
> validates\_presence\_of :name, :if =\> Proc.new { |r| r.parent }
> 
> – Jean-FranÃ§ois.
> 
> –  
> Ã? la renverse.

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 29, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/selective-validation-is-it-possible/74032/5 "2006-10-29T12:06:08Z")

</div>

Nevermind, I figured it out. 🙂

Thanks a lot!  
Petr

Petr wrote:

> Hey, thanks. I have one more question, could you describe to me what  
> that does in detail? I know Proc objects from Ruby, I just cant imagine  
> how it works in this case.
> 
> Petr
> 
> Jean-FranÃ§ois wrote:
> 
> > Hello Petr,
> > 
> > > I have a Product model and products table. Im using acts\_as\_tree  
> > > structure. I would like active record validation to be done on children  
> > > only. Anyway to do this?
> > 
> > The root product is the product who has no parent.  
> > So in your validation, you could pass an :if option and a condition  
> > in the block :
> > 
> > :if =\> Proc.new { |r| r.parent }
> > 
> > Example :  
> > validates\_presence\_of :name, :if =\> Proc.new { |r| r.parent }
> > 
> > – Jean-FranÃ§ois.
> > 
> > –  
> > Ã? la renverse.

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 29, 2006, 12:16pm UTC](https://www.ruby-forum.com/t/selective-validation-is-it-possible/74032/6 "2006-10-29T12:16:41Z")

</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.

– Jean-FranÃ§ois.

–  
Ã? la renverse.
