3
我第一次使用mongoid,并且在创建工厂中的has_many关联时遇到了问题。Mongoid has_many association and factorygirl
的情况是这样的:
我有一组类:
class Group
include Mongoid::Document
field :name, :type => String
end
而且我有一个健身班。练习可以属于许多组。目前运动类定义如下:
class Exercise
include Mongoid::Document
field :name, :type => String
field :description, :type => String
has_many :groups
validates_presence_of :name, :description
end
我想使用factorygirl为specs创建实例。我正在努力如何做到这一点。
目前我的锻炼工厂看起来像这样;
FactoryGirl.define do
factory :exercise do
name "Preacher curls"
description "Do something"
after(:build) do |exercise|
exercise.groups << FactoryGirl.build(:group)
end
end
end
这会导致以下错误:
NoMethodError: undefined method `=' for #<Group _id: 4fbc6f5a26a3181742000004, _type: nil, name: "Arms">
如何创建锻炼工厂正确添加group_ids?