Selective validation? Is it possible?

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

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.

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

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.

Nevermind, I figured it out. :slight_smile:

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.

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.