Share enum declaration values with multiple attributes
ruby-on-rails, ruby-on-rails-4
Solution
As of Rails 5.0 you can use the `_prefix` or `_suffix` options when you need to define multiple enums with same values. If the passed value is true, the methods are prefixed/suffixed with the name of the enum.
class Invoice < ActiveRecord::Base
enum verification: [:done, :fail], _prefix: true
end
It is also possible to supply a custom prefix.
class Invoice < ActiveRecord::Base
enum verification: [:done, :fail], _prefix: :verification_status
end
Problem
I want to have a class with several attributes that saves weekdays with numeric values. ``` summary_weekday :integer collection_weekday :integer ``` I thought I could map the integers to values using Enum with two declarations: ``` enum summary_weekday: %w(monday tuesday wednesday thursday friday saturday sunday) enum collection_weekday: %w(monday tuesday wednesday thursday friday saturday sunday) ``` But Rails doesn't accept that, I cannot define the same value twice in the same class. You tried to define an enum named "summary_weekday" on the model "QuestionCategory", but this will generate a instance method "monday?", which is already defined by another enum. How can I solve this?