FactoryGirl属于_to协会

bon*_*fer 5 ruby-on-rails mongoid factory-bot

我有一个工厂,我在其中定义了一个位置factories/locations.rb.我正在使用Mongoid和Rails 3.1.1和ruby 1.9.3.

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

然后我想定义一个属于一个位置的健身营(因此具有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

我错过了什么吗?这是否表示我的模型可能配置不正确?

谢谢!

蒂姆

bon*_*fer 5

我是个白痴 - 我有 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

雷达(Ryan Bigg)的疯狂道具帮助我度过了这个.