Skip to content

Instantly share code, notes, and snippets.

@ejlangev
Created July 29, 2014 19:00
Show Gist options
  • Save ejlangev/f67fadb4b5497d897452 to your computer and use it in GitHub Desktop.
Save ejlangev/f67fadb4b5497d897452 to your computer and use it in GitHub Desktop.
Foreign Key Validator
class ForeignKeyValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value && value.to_s =~ /^\d+$/ && value.to_i > 0
record.errors[attribute] << 'is not a valid foreign key'
end
end
end
@brett-richardson
Copy link

👍

@alexslade
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment