문제

I'm using ActiveRecord's Enum to store the weekdays with the following:

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

When calling the attribute .weekday on the model instance I correctly get the weekday name, e.g. "monday".

How could I get the numeric value (i.e. 0) when I need that instead?

도움이 되었습니까?

해결책

Use my_object[:weekday], or, if you're in the object, just self[:weekday].

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

다른 팁

What you would rather want to do is this:

model_instance.read_attribute(:weekday)

Cleaner, simpler, straight to the point.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top