获取必填字段列表

时间:2017-03-21 13:15:28

标签: ruby-on-rails

我使用以下方法将Post模型的所有属性显示为HTML表格标题:

Post.column_names #=> ['id','name','status', 'created_at',....]

但我只需选择Post模型中所需的字段,即validates: presence: true

有没有办法选择必填字段?

1 个答案:

答案 0 :(得分:4)

试试这个,

Post.validators

它将返回模型中所有声明的验证器。 喜欢 <ActiveModel::Validations::PresenceValidator:0x00000007b12568 @attributes=[..]

您可以迭代它或直接访问Post.validators.first.attributes

希望它能为你提供帮助。