Ruby: How do you configure an enum in a fixture?

烈酒焚心 提交于 2019-12-01 17:34:11

问题


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?


回答1:


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] %>


来源:https://stackoverflow.com/questions/29290908/ruby-how-do-you-configure-an-enum-in-a-fixture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!