'enum option "new" not working

I am trying to create an enum on my model and I would like one of the states to be "new"

e.g.

enum status: { stale: 0, new: 1, converted: 2 }

It seems rails rejects this with the following error.

You tried to define an enum named "status" on the model "Lead", but this will generate a class method "new", which is already defined by Active Record.

I understand why this is happening, but I was wondering if there is no way to get around this?



Solution 1:[1]

The error clearly states that you cannot have an enum with new key as it will conflict with an existing ActiveRecord method. There is no way out of this.

This issue is not new and it has been discussed before.

I would recommend you to read

enum: Option not to generate "dangerous" class methods

As per Godfrey Chan, Collaborator of Rails:

In this case, if you want to use enum, you are probably better off renaming your label to something else. This is not unique to enums – a lot of Active Record features generates methods for you and usually there aren't ways to opt-out of those generated methods.

Gonna give this one a close for now....

Solution 2:[2]

I solved my problem by leaving config / environments / production.rb as default and everything returned to normal without errors

Solution 3:[3]

This issue occurs due to reserved words in Rails. To solve this issue, you can use two options:

  1. Rename the attribute status to another non reserved word. E.g.: kind

  2. Or just add the _prefix: :status in the enum. E.g: enum status: { stale: 0, new: 1, converted: 2 }, _prefix: :status

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 gilcierweb
Solution 3 Darlan Dieterich