Created
March 11, 2016 17:47
-
-
Save hugohernani/7594af4592201cd77d97 to your computer and use it in GitHub Desktop.
Get Attributes which presence validator was added to a model_(instance|klass)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It does not work if the attribute is using a presence validator under certain conditional