Factory Girl with a serialized array "TypeError: cannot pass array to text"

This very close question answer does not work for me: Factory Girl with serialized field

I am trying to use Factory Girl to set an Array serialized field in a nested Factory and getting:

TypeError:
    can't cast Array to text

Models:

class Transmission < ActiveRecord::Base
end

class Letter < Transmission
  serialize :genders, Array
end

Factory:

FactoryGirl.define do
  factory :transmission do
    factory :letter do
      genders ["male", "female"]
    end
  end
end

in spec

FactoryGirl.create(:letter)

the gender field has a text type in db messages

factory_girl 4.4.0

+4
source share
1 answer

The result FactoryGirl.create (: letter) was a transfer instance that does not have:

serialize :genders, Array

I had to add the class name to the nested factory as follows:

FactoryGirl.define do
  factory :transmission do
    factory :letter, class: Letter do
      genders ['male', 'female']
    end
  end
end
+3
source

All Articles