ruby on rails - Validating models against the parent model -


i have 2 models, 1 parent of other, , parent accepts_nested_attributes_for , validates_associated children.

however, of validations have :if needs check 1 of properties of parent.

i thinking this:

validates_presence_of :blah, :if => proc.new{|thing| thing.parent.some_value.present?} 

however, 'parent' relationship doesn't appear setup @ time of validation (i assume children instantiated , validated first.

therefore there way of doing i'm thinking of? possible?

you can use before_update or before_create callbacks per need this..

def before_update   self.errors.add("error message") if self.parent.some_value.present?   return false if self.errors.count > 0 end  def before_create   self.errors.add("error message") if self.parent.some_value.present?   return false if self.errors.count > 0 end 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -