Skip to content

Instantly share code, notes, and snippets.

@jvsidler
Created August 10, 2011 16:18
Show Gist options
  • Save jvsidler/1137314 to your computer and use it in GitHub Desktop.
Save jvsidler/1137314 to your computer and use it in GitHub Desktop.
Ruby on Rails: Custom Validator
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