代码改变世界

Rails 3 validates验证器

2013-07-13 02:21  张小萌  阅读(185)  评论(0编辑  收藏  举报

之前:

def validate

  if baggage >flight.baggage_allowance

     errors.add_to_base("You have too much baggage")

end 

if flight.seats.size >= flight.capacity

  errors.add_to_base("The flight is fully booked")

end

end

end

 

Rails 3:

validate :baggage_does_not_exceed_baggage_capacity, :seats_available

def baggage_does_not_exceed_baggage_capacity

errors.add(:baggage, ": The baggage limit is #{flight.baggage_allowance}") if baggage > flight.baggage_allowance

end

def seats_available

errors.add(:flight_id, "The flight is booked") if flight.seats.size >= flight.capacity

end