【发布时间】:2017-04-11 08:17:48
【问题描述】:
我有一个具有枚举作为属性的模型。
class ApplicationLetter < ActiveRecord::Base
belongs_to :user
belongs_to :event
validates :user, :event, presence: true
enum status: {accepted: 1, rejected: 0, pending: 2}
end
以及生成此模型并为枚举设置值的工厂
FactoryGirl.define do
factory :application_letter do
motivation "motivation"
user
event
status :accepted
end
end
在控制器测试中我想通过工厂获取有效属性
let(:valid_attributes) { FactoryGirl.build(:application_letter).attributes }
并使用这些属性创建一个应用程序。
application = ApplicationLetter.create! valid_attributes
但我收到以下错误:
参数错误: “1”不是有效状态
为什么状态被解释为字符串?如果我在工厂更改状态,我会得到相同的错误,但对应的编号正确。
【问题讨论】:
-
看起来像 stackoverflow.com/questions/27606297/… 的副本,但那里也没有好的答案。如果问题仍然存在,可能需要在 GitHub 上报告此问题。
标签: ruby-on-rails ruby ruby-on-rails-4 enums factory-bot