Skip to content

Instantly share code, notes, and snippets.

@hugohernani
Created March 11, 2016 17:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugohernani/7594af4592201cd77d97 to your computer and use it in GitHub Desktop.
Save hugohernani/7594af4592201cd77d97 to your computer and use it in GitHub Desktop.
Get Attributes which presence validator was added to a model_(instance|klass)
def required_attributes(obj)
target = obj.class == Class ? obj : obj.class
target.validators.select do |amv|
amv.class == ActiveRecord::Validations::PresenceValidator
end.map(&:attributes).flatten
end
@hugohernani
Copy link
Author

It does not work if the attribute is using a presence validator under certain conditional

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