我使用以下方法将Post
模型的所有属性显示为HTML表格标题:
Post.column_names #=> ['id','name','status', 'created_at',....]
但我只需选择Post
模型中所需的字段,即validates: presence: true
有没有办法选择必填字段?
答案 0 :(得分:4)
试试这个,
Post.validators
它将返回模型中所有声明的验证器。
喜欢
<ActiveModel::Validations::PresenceValidator:0x00000007b12568 @attributes=[..]
您可以迭代它或直接访问Post.validators.first.attributes
希望它能为你提供帮助。