3

私はシーケンスを持つ非常に単純な工場を持っています

FactoryGirl.define do

  factory :lecture do
    sequence(:name) {|n| "lecture #{n}" }
    sequence(:slug) {|n| "lecture-#{n}" }
  end
end

buildがそれをするとき - それはうまく見えます

(FactoryGirl.build :lecture)
=> #<Lecture id: nil, slug: "lecture-20", name: "lecture 20", position: nil, created_at: nil, updated_at: nil>

しかし、にアクセスしようとすると、値attributesしか取得できませんnil

(FactoryGirl.build :lecture).attributes
=> {"name"=>nil,
 "slug"=>nil,
 "id"=>nil,
 "position"=>nil,
 "created_at"=>nil,
 "updated_at"=>nil}

シーケンスなしで別のファクトリを使用すると、正常に動作します。また、使用してもattributes_for問題なく動作します

FactoryGirl.attributes_for(:lecture)
=> {:name=>"lecture 23", :slug=>"lecture-23"}

使用できるようにしたいので、 foreign-keysを含むbuildすべての属性を取得しますが、どういうわけか値を返します...何が間違っているのか、またはそれを修正する方法はありますか?nil

4

0 に答える 0