Rails range validation with data from another model -
i have ticket , event model in relationship event has many tickets. ticket model has columns serial starting value , serial ending value denotes range of valid serial nubers event.
i want validate ticket on creation if ticket created serial number beyond range, system spew out errors saying range in invalid.
what have in ticket model validation validation show valid serial number events between 140000 , 149999
validates :serial_number, presence: true, numericality: { only_integer: true, :greater_than_or_equal_to => 140000, :less_than => 149999, :message => "is invalid" }, :uniqueness => { :message => "%{value} has been taken" }
i need data event model , place ticket validation. rails allow in model? should in controller?
definitely in model. use custom validations it:
validate :serial_number_valid def serial_number_valid unless event.serial_number_range.include?(serial_number) errors.add(:serial_number, "must within range") end end
Comments
Post a Comment