The enum defined like this:

enum status: %i[pending unfinished finished]

You can get the integer like so:

< Rails 5

my_model = MyModel.find(123)
my_model[:status] # Returns the integer value

Rails 5+

my_model.status_before_type_cast # Returns the integer value

Also, you can get the integer values for an enum from the class the enum is on:

MyModel.statuses
# =>  {"pending"=>0, "unfinished"=>1, "finished"=>2}
my_model = MyModel.find(123)
MyModel.statuses[my_model.status] # Returns the integer value
0条评论 顺序楼层
请先登录再回复