Using Enum and want to access the stored integer value in the view

I use ActiveRecord Enum to store weekdays with the following:

enum weekday: %w(monday tuesday wednesday thursday friday saturday sunday)

When calling the attribute .weekdayin the model instance, I correctly get the name of the day of the week, for example. "Monday".

How can I get a numerical value (i.e. 0) when I need it?

+4
source share
2 answers

Use my_object[:weekday], or, if you are in the facility, simple self[:weekday].

UPDATE: OR (as found by Ms numbers): .read_attribute_before_type_cast(:weekday)

+9
source

What you would like to do is the following:

model_instance.read_attribute(:weekday)

, , .

+3

Source: https://habr.com/ru/post/1538480/


All Articles