Advertisement
saasbook

conditional_validation_example.rb

Feb 20th, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.41 KB | None | 0 0
  1. class Movie < ActiveRecord::Base
  2.   RATINGS = %w[G PG PG-13 R NC-17]  #  %w[] shortcut: arrays of strings
  3.   @@grandfathered = Date.parse('1-Nov-1968')
  4.   validates_inclusion_of :rating, :in => RATINGS,
  5.     :if => lambda { |m| m.release_date >= @@grandfathered }
  6.   # another way:
  7.   validates_inclusion_of :rating, :in => RATINGS, :unless => :grandfathered
  8.   def grandfathered ; release_date <= @@grandfathered ; end
  9. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement