Rails enum field

Hello!
We’re integrating Forest admin with our Rails app using Lumber.

The default Rails implementation of an Enum uses an Integer DB-field which is managed by some Rails magic on top of it.

In Lumber the Integer field becomes a Number field automatically and I cannot find a simple way to specify that the field value “0” should display as “Enum value 1” etc.

Is it correct that getting this output to work requires setting up a Smart field through Lumber?

Hello @svensson-david !
Can you give me an example of your enum ?
If I understood correctly, you are using a lumber generated project alongside your rails app, if that’s the case then you should be able to display the value correctly :thinking:

Hi @anon94532230

In the rails app the enum is defined as:

# app/models/organization.rb

class Organization < ApplicationRecord
  ...
  enum account_type: { maritime_industry: 0, training_provider: 1, school: 2 }
  ...
end

This is backed by an integer in the DB.

# db/schema.rb

...
create_table "organizations", force: :cascade do |t|
  ...
  t.integer "account_type", default: 0
  ...
end
...

In Lumber this was generated as:

# .forestadmin-schema.json
{
    "field": "accountType",
    "type": "Number",
    "defaultValue": null,
    "enums": null,
    "integration": null,
    "isFilterable": true,
    "isReadOnly": false,
    "isRequired": false,
    "isSortable": true,
    "isVirtual": false,
    "reference": null,
    "inverseOf": null,
    "validations": []
}

I hope that clarifies things!
Best,
David

Yes it does :slight_smile:
Can you share the generated model file in your lumber project ?

Sure, here’s the relevant part

# models/organizations.js

module.exports = (sequelize, DataTypes) => {
    const { Sequelize } = sequelize;
    const Organizations = sequelize.define('organizations', {
      ...
      accountType: {
        type: DataTypes.INTEGER,
      }
      ...
    }, {
      tableName: 'organizations',
      underscored: true,
      schema: process.env.DATABASE_SCHEMA,
    });
  };
  

If I understand correctly, yo don’t have an enum in your database ? You are defining it in your main app ? If that’s the case then yes you have to use a smart field to map your values :slight_smile:

Yes exactly:

But anyway good, then I know.
Thanks!