好像我应该能够在几个小时的谷歌搜索和测试后找到明显的答案。工厂女孩的属性
我希望能够在caredate工厂内设置caredate.user_id => provider.user_id。
测试错误:
我有一个ActiveRecord验证时,通过浏览器进行测试,其工作原理:
class Caredate < ActiveRecord::Base //works fine when testing via browser
belongs_to :user
belongs_to :provider
validates_presence_of :user_id
validates_presence_of :provider_id
validate :user_must_be_same_as_provider_user
def user_must_be_same_as_provider_user
errors.add(:user_id, "must be same as provider user") unless self.user_id == self.provider.user_id
end
end
//factories.rb
Factory.define :user do |f|
f.password "test1234"
f.sequence(:email) { |n| "foo#{n}@example.com" }
end
Factory.define :caredate do |f|
f.association :provider
**f.user_id { Provider.find_by_id(provider_id).user_id } //FAILS HERE**
end
Factory.define :provider do |f|
f.association :user
end
我的道歉,如果这以前已经回答;我尝试了几种不同的选择,但无法实现。
更新:这通过验证,所以我越来越近。我可以用随机数破解。
Factory.define :caredate do |f|
f.association :user, :id => 779
f.association :provider, :user_id => 779
end
如果提供程序工厂在cadeate工厂之前在factories.rb中定义,则工作。谢谢! – 2010-12-11 14:55:48
订单依赖关系是一个很好的观点,如果两个工厂是在单独的文件中定义的,则更为糟糕。在某些时候,你想知道在这种情况下使用灯具是否更有意义。 – zetetic 2010-12-11 19:37:20
这工作,直到我试图重建数据库和加载架构。看起来FactoryGirl正在寻找餐桌。 'rake db:schema:load --trace ... rake中止! 表'claimaway_development.providers'不存在'[Here](http://groups.google.com/group/factory_girl/browse_thread/thread/4852f1a129d72839)是一个讨论,但我不能得到这个解决方案的工作。 – 2011-03-20 19:33:56