Given this class:
鉴于此课程:
class User < ActiveRecord::Base
enum permission: {
permission_user: 1,
permission_staff: 2,
permission_manager: 3,
permission_admin: 4,
permission_super_admin: 5
}
I want to create a fixture that looks like this:
我想创建一个看起来像这样的夹具:
testuser1:
id: 1
username: sam
permission: :permission_staff
I've tried a number of variations of syntax, but haven't found something that works. the resulting user.permission is either nil or 0. I know that enum is relatively recent addition. Can this be done?
我已经尝试了许多语法变体,但没有找到有用的东西。结果user.permission是nil或0.我知道枚举是相对最近的添加。可以这样做吗?
1 个解决方案
#1
12
According to the enum docs you can refer to the enumerable through the class like this:
根据枚举文档,你可以像这样引用类中的可枚举:
User.permissions[:permission_staff]
And the factories are just ruby code - so they should be able to access the value in the same way
而工厂只是红宝石代码 - 所以他们应该能够以同样的方式访问价值
testuser1:
id: 1
username: sam
permission: <%= User.permissions[:permission_staff] %>
#1
12
According to the enum docs you can refer to the enumerable through the class like this:
根据枚举文档,你可以像这样引用类中的可枚举:
User.permissions[:permission_staff]
And the factories are just ruby code - so they should be able to access the value in the same way
而工厂只是红宝石代码 - 所以他们应该能够以同样的方式访问价值
testuser1:
id: 1
username: sam
permission: <%= User.permissions[:permission_staff] %>