Rails validate before save


/ Published in: Rails
Save to your folder(s)



Copy this code and paste it in your HTML
  1. class Product < ActiveRecord::Base
  2. validate :must_go_on_sale_before_event_starts
  3.  
  4. # has scheduled_for datetime
  5.  
  6.  
  7. protected
  8. def must_go_on_sale_before_event_starts
  9. errors.add(:scheduled_for, 'should be before starts at') if scheduled_for > behaviour.starts_at || behaviour.starts_at < scheduled_for
  10. end
  11. end
  12.  
  13. class Product::Event < ActiveRecord::Base
  14. # has starts_at datetime

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.