rails validates_associated if/unless not working -
i have parent has_many association children. child class has of own validations. want child validations run if value on parent set true. reason, no matter try validations on child run.
parent class
class parent < activerecord::base has_many :children accepts_nested_attributes_for :children, allow_destroy: true, reject_if: :all_blank validates_associated :children, if: proc.new { |m| m.do_validation == true } end child class
class child < activerecord::base validates_presence_of :name end am using validates_associated incorrectly here?
thanks
validates_associated perform validations on associated records, in context of caller. in example when create parent if validations on parent pass, call validations on child ( related children), , if of validations fail, create of parent fail.
this means when create child call validations on child, not mater have put if in parent class, because validations related child model.
to prevent validations of child executed every time when record created, updated, ... have add if clause in child model.
Comments
Post a Comment