3

I'm showing all attributes of a Post model as an HTML table header by using:

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

But I need to pick only those fields that are required in Post model i.e having validates: presence: true

Is there a way to pick the required fields?

Arif
  • 1,369
  • 14
  • 39

1 Answers1

5

Try this,

Post.validators

It will return all declared validator in your Model. like <ActiveModel::Validations::PresenceValidator:0x00000007b12568 @attributes=[..]

You can iterate over it or directly access like Post.validators.first.attributes

Hope It's help you.

Hardik Upadhyay
  • 2,639
  • 1
  • 19
  • 34