gillien
gillien

Reputation: 412

How to find all enum fields in a class in Rails

Today, we are able to get fields type for a lambda class by doing : User.columns.map { |c| [c.name, c.type]}

If a field is an enum, it will return the type integer. Is there anyway to find which fields are set as an enum from an active record class?

Upvotes: 1

Views: 1932

Answers (2)

adesurirey
adesurirey

Reputation: 2620

User.columns.map { |c| [c.name, c.type, User.defined_enums[c.name]].compact }

Upvotes: 0

ray
ray

Reputation: 5552

You can get it as (provided by ActiveRecord::Enum),

User.defined_enums.keys

Upvotes: 8

Related Questions