Skip to content

Instantly share code, notes, and snippets.

@mattantonelli
Created March 12, 2015 19:35
Show Gist options
  • Select an option

  • Save mattantonelli/5d3dd4cca01ec4bc5f9a to your computer and use it in GitHub Desktop.

Select an option

Save mattantonelli/5d3dd4cca01ec4bc5f9a to your computer and use it in GitHub Desktop.
# Validate fields in different ActiveRecord objects with the same
# validation class without having to share the same field name.
#
# Field name is provided in the controller as follows:
# validates_with MyValidator, field: :field_name
#
def validate(record)
field = options[:field]
if !(valid_field?(record[field]))
record.errors.add(field, "invalid_#{field}".to_sym)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment