如何从Rails中的枚举中获取整数值?

时间:2022-05-01 20:54:01

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.

但是这个只返回0。

5 个解决方案

#1


98  

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:

然后,您可以使用模型类实例中的sale_info值来访问该实例的整数值:

my_model = Model.find(123)
Model.sale_infos[my_model.sale_info] # Returns the integer value

#2


101  

You can get the integer like so:

可以得到这样的整数:

my_model = Model.find(123)
my_model[:sale_info] # Returns the integer value

Update for rails 5

rails更新为5

For rails 5 the above method now returns the string value :(

对于rails 5,上面的方法现在返回字符串值:(

The best method I can see for now is:

目前我能看到的最好的方法是:

my_model.sale_info_before_type_cast

Shadwell's answer also continues to work for rails 5.

Shadwell的回答同样适用于rails 5。

#3


17  

Rails < 5

Another way would be to use read_attribute():

另一种方法是使用read_attribute():

model = Model.find(123)
model.read_attribute('sale_info')

Rails >= 5

You can use read_attribute_before_type_cast

您可以使用read_attribute_before_type_cast

model.read_attribute_before_type_cast(:sale_info)
=> 1

#4


0  

My short answer is Model.sale_infos[:plan_2] in case if you want to get value of plan_2

我的简短回答是模型。sale_infos[:plan_2]如果你想要获取plan_2的值

#5


0  

I wrote a method in my Model to achieve the same in my Rails 5.1 app.

我在模型中编写了一个方法,以便在Rails 5.1应用程序中实现同样的功能。

Catering for your case, add this into your Model and call it on the object when needed

满足您的情况,将它添加到模型中,并在需要时在对象上调用它

def numeric_sale_info
  self.class.sale_infos[sale_info]
end

#1


98  

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:

然后,您可以使用模型类实例中的sale_info值来访问该实例的整数值:

my_model = Model.find(123)
Model.sale_infos[my_model.sale_info] # Returns the integer value

#2


101  

You can get the integer like so:

可以得到这样的整数:

my_model = Model.find(123)
my_model[:sale_info] # Returns the integer value

Update for rails 5

rails更新为5

For rails 5 the above method now returns the string value :(

对于rails 5,上面的方法现在返回字符串值:(

The best method I can see for now is:

目前我能看到的最好的方法是:

my_model.sale_info_before_type_cast

Shadwell's answer also continues to work for rails 5.

Shadwell的回答同样适用于rails 5。

#3


17  

Rails < 5

Another way would be to use read_attribute():

另一种方法是使用read_attribute():

model = Model.find(123)
model.read_attribute('sale_info')

Rails >= 5

You can use read_attribute_before_type_cast

您可以使用read_attribute_before_type_cast

model.read_attribute_before_type_cast(:sale_info)
=> 1

#4


0  

My short answer is Model.sale_infos[:plan_2] in case if you want to get value of plan_2

我的简短回答是模型。sale_infos[:plan_2]如果你想要获取plan_2的值

#5


0  

I wrote a method in my Model to achieve the same in my Rails 5.1 app.

我在模型中编写了一个方法,以便在Rails 5.1应用程序中实现同样的功能。

Catering for your case, add this into your Model and call it on the object when needed

满足您的情况,将它添加到模型中,并在需要时在对象上调用它

def numeric_sale_info
  self.class.sale_infos[sale_info]
end