Enum option "new" does not work

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

eg.

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

The rails seem to reject it 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 was a way around this?

+7
enums ruby-on-rails-4
source share
2 answers

This error explicitly states that you cannot list with the new key, because it will conflict with the existing ActiveRecord method. This is not the way out of this.

This problem is not new, and it has been discussed previously.

I would recommend you read

enum: option not to create โ€œdangerousโ€ class methods

According to Godfrey Chan , Collaborator of Rails:

In this case, if you want to use an enumeration, you are probably better off renaming your shortcut to something else. This is not unique to enumerations - many Active Record functions generate methods for you, and there is usually no way to discard these generated methods.

Let's close it now ...

+13
source share

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

0
source share

All Articles