2011-11-13 2 views
5

私はfactories/locations.rbに位置を定義する工場を持っています。私はMongoidとRails 3.1.1をRuby 1.9.3で使用しています。FactoryGirl belongs_to associ

 
FactoryGirl.define do 
    factory :location do 
     name Faker::Name.name 
     description "Down by the river" 
    end 
end 

そして、私は場所をBELONGS_TO(したがってlocation_idの属性を持つ)フィットネスキャンプを定義したいです。

 
FactoryGirl.define do 
    factory :fitness_camp do 
    title "Parkour" 
    association :location_id, :factory => :location 
    end 
end 

この私はドキュメントで読んだもの、ないを動作しますが、私のハッキングの結果です。入門ガイド(https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md)から、のように単純である必要がありそうです:

 
    factory :fitness_camp do 
    title "Parkour" 
    location 
    end 

私は何かが足りないのですか?私のモデルが正しく構成されていない可能性がありますか?

ありがとうございます!

ティム

+0

ここを見てください:http://robots.thoughtbot.com/post/9713995255/factorygirl-2-1-0-brings-the-heat – apneadiving

+0

私はそこを見ました - まだ不明です。私は今日後でソースコードを見ていきます。 – bonhoffer

+0

どのFactoryGirlのバージョンを使用していますか? – sevenseacat

答えて

5

は私が馬鹿だった - 私はこれを通じて、私を助けるためレーダー(ライアンBiggの)にvalidates_numericality_of :location_id

 
class FitnessCamp 

    include Mongoid::Document 

    field :title, :type => String 

    belongs_to :location 

    validates_presence_of :location_id, :title 
    validates_numericality_of :location_id 

マッド小道具を持っていました。