Querenker Querenker - 6 months ago 29
Ruby Question

Rails 4: Set enum field through FactoryGirl attributes

I have a Model that has an enum as attribute.

class ApplicationLetter < ActiveRecord::Base
belongs_to :user
belongs_to :event

validates :user, :event, presence: true

enum status: {accepted: 1, rejected: 0, pending: 2}


As well as a factory that generates this model and sets a value for the enum

FactoryGirl.define do
factory :application_letter do
motivation "motivation"
status :accepted

In a controller test I want to get valid attributes through the factory

let(:valid_attributes) { FactoryGirl.build(:application_letter).attributes }

and create a application with these attributes.

application = ApplicationLetter.create! valid_attributes

But I get the following error:

'1' is not a valid status

Why is status interpreted as string? If I change the status in the factory I get the same error, but with the right corresponding number.

Answer Source
let(:valid_attributes) { FactoryGirl.build(:application_letter).attributes.merge(status: 'accepted') }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download