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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---