Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share enum declaration values with multiple attributes

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?

like image 942
Fellow Stranger Avatar asked May 17 '14 14:05

Fellow Stranger


People also ask

How do you define an enum with multiple values?

we should do the following steps to have an enum with different values: Create enum constructor which accepts multiple values. Assign each constructor argument to a member field in the enum definition. Create getter methods so we can access any of the values assigned to a particular enum constant.

Can we assign multiple values to enum?

Assigning Multiple ValuesBy including the FlagsAttribute attribute in the Enum declaration, you can instead assign multiple values to an instance of the enumeration. The FlagsAttribute attribute specifies that the enumeration be treated as a bit field, that is, a set of flags. These are called bitwise enumerations.

Can enum have multiple values C++?

An enum instance may hold multiple values only as much as an int can: that is, not at all.

Can two enum names have the same value?

1. Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.


2 Answers

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 
like image 148
Giles Bathgate Avatar answered Sep 22 '22 14:09

Giles Bathgate


What you are looking for cannot be obtained by enum, as it will try to create methods like object.tuesday? for both of the enums, which is logically wrong.

I recall DataMapper ORM to have exactly this kind of support. For ActiveRecord, you will probably have to create getter and setter methods manually for these properties and map it there, like:

WEEKDAYS = %w(monday tuesday wednesday thursday friday saturday sunday)  def summary_weekday   WEEKDAYS[read_attribute(:summary_weekday).to_i]  end  def summary_weekday=(value)   write_attribute(:summary_weekday, WEEKDAYS.index(value.to_s)) end 

The code above may need some mangling while typecasting etc. I know that this is not a generic proof and I will really love to know any better solution for this interesting problem; will certainly be very useful.

Courtesy - Ruby Forum

Hope this helps.

like image 36
kiddorails Avatar answered Sep 19 '22 14:09

kiddorails