1
我试图让FactoryGirl中的协会工作,他们只是......不。我已经基本上得到了这个:在FactoryGirl中获得belongs_to协会的权利
class Foo
include Mongoid::Document
belongs_to :bar
end
class Bar
include Mongoid::Document
has_many :foos
end
FactoryGirl.define do
factory :foo, class => Foo do
bar
end
factory :bar, class => Bar do
end
end
至少这样的文档使我相信......但后来在我的测试,我有
a_foo=FactoryGirl.create :foo
a_foo.bar # Hooray! It's an associated object
Foo.where(_id: a_foo._id).includes(:bar).first.bar # This is nil!
为什么是最后一个关联的值为零线?我不需要它,因为被测试的实际代码做了同样的事情,并且它有权期望它能够正常工作......我错过了为什么这种方法无法正常工作?可能是与急切的加载有关吗?
没有'has_one'语句? – apneadiving
我不经常在MongoDB中使用Rails,但我假设Foo.find(id)仍然有效,是吗?如果是这样,当你尝试'Foo.find(a_foo._id).bar'时会发生什么? – Samo
对不起,我编辑了我的帖子以表明真实代码具有'has_many'关系。 @Samo,它是零,就像在我的问题中的代码行... – cbmanica