Skip to content

Instantly share code, notes, and snippets.

@cloudsben
Last active July 26, 2016 11:50
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 cloudsben/7a33a40c60a309d9f84e507f71becaf2 to your computer and use it in GitHub Desktop.
Save cloudsben/7a33a40c60a309d9f84e507f71becaf2 to your computer and use it in GitHub Desktop.
rails 查询字段是否为验证字段
# 查询字段是否为验证字段,如果是验证字段返回 true
def presence?(poi, field)
presence_fields = poi.constantize.validators.map do |v|
v.attributes if v.is_a? ActiveRecord::Validations::PresenceValidator
end
# 去 nil 并 二维转一维
if presence_fields.compact.flatten.include?(field)
return true
else
return false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment