Rails - Validate field in another model -


need on figuring out how validate 1 field if , if field in related model of value. example:

//my models

class course < activerecord::base   has_many :locations, :dependent => :destroy   accepts_nested_attributes_for :locations end  class location < activerecord::base   belongs_to  :course end 

a course can have many locations (state, city, etc) start_date. want have like: "allow location.start_date blank if course.format == 'dvd'"

in location model tried like:

validates_presence_of   :start_date,                         :message => "start date can't blank",                         :allow_blank => false,                         :if => proc.new { |course| self.course.format != 'dvd' } 

then when use that, get: private method 'format' called nil:nilclass

not sure if i'm on right track here.

thanks!

the proc passed if clause passes parameter block instance of current object being validated. so, have |course| |location|. try following , see if want:

validates_presence_of   :start_date,                         :message => "start date can't blank",                         :allow_blank => false,                         :if => proc.new { |location| location.course.format != 'dvd' } 

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -