I have a enum in my Model that corresponds to column in the database.
The enum
looks like:
enum sale_info: { plan_1: 1, plan_2: 2, plan_3: 3, plan_4: 4, plan_5: 5 }
How can I get the integer value?
I've tried
Model.sale_info.to_i
But this only returns 0.
You can get the integer values for an enum from the class the enum is on:
Model.sale_infos # Pluralized version of the enum attribute name
That returns a hash like:
{ "plan_1" => 1, "plan_2" => 2 ... }
You can then use the sale_info value from an instance of the Model
class to access the integer value for that instance:
my_model = Model.find(123)
Model.sale_infos[my_model.sale_info] # Returns the integer value
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加