Skip to content

Instantly share code, notes, and snippets.

@kikito
Created September 10, 2015 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kikito/c3da655d20cd0c0bbef2 to your computer and use it in GitHub Desktop.
Save kikito/c3da655d20cd0c0bbef2 to your computer and use it in GitHub Desktop.
Comment length validation options
class Comment < ActiveRecord::Base
validate do
validator = ActiveModel::Validations::LengthValidator.new(
attributes: :body,
minimum: 10,
maximum: Comment.body_max_length)
validator.validate(self)
end
def self.body_max_length
1000
end
end
class Comment < ActiveRecord::Base
validate :validate_body_length
def self.body_max_length
1000
end
private
def validate_body_length
validator = ActiveModel::Validations::LengthValidator.new(
attributes: :body,
minimum: 10,
maximum: Comment.body_max_length)
validator.validate(self)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment