Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How should my Rails before_validation callbacks handle bad data?
- class Foo < ActiveRecord::Base
- before_validation :capitalize_title
- validates :title, :presence => true
- def capitalize_title
- title.upcase
- end
- end
- def capitalize_title
- title = title.upcase if title
- end
- before_validation :mangle_data
- validate :data_is_okay
- #...
- def mangle_data
- return if(!data_is_mangleable)
- #... mangle away
- end
- def date_is_okay
- if(!data_is_mangleable)
- # complain
- end
- if(!data_is_properly_mangled)
- # complain some more
- end
- end
- def data_is_mangleable
- return false if(thing.nil?)
- # etc.
- end
- def data_is_properly_mangled
- # check that stuff that the before_validation hook doesn't
- # have to care about.
- end
Add Comment
Please, Sign In to add comment