Created
August 10, 2011 16:18
-
-
Save jvsidler/1137314 to your computer and use it in GitHub Desktop.
Ruby on Rails: Custom Validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, value) | |
# If the relationship exists and the related object is active | |
unless eval("record.#{attribute.to_s.gsub("_id", "")}") && eval("record.#{attribute.to_s.gsub("_id", "")}").active | |
record.errors.add(attribute, "must be an active #{attribute.to_s.gsub("_id", "")}") unless eval("record.#{attribute}.blank?") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment