工厂女郎协会

时间:2012-08-13 01:23:26

标签: ruby-on-rails rspec

我有广告工厂,但如何将其分配给user.ads属性?我试过了

@ad = user.ads.build(:ad)

但这对我不起作用

FactoryGirl.define do
 factory :user do
  name     "My"
  email    "my@example.com"
 end

 factory :ad do
  title "Ola"
  description "Hello"
  user
 end
end


let(:user) { FactoryGirl.create(:user) }
#so i don't type it like this:
@ad = user.ads.build(title: "Lorem ipsum",description: "Lorem ipsum")
subject { @ad }

1 个答案:

答案 0 :(得分:1)

let(:user) { FactoryGirl.create(:user) }    
5.times { FactoryGirl.create(:ad, user: user) }

user.ads #=> array of ads

当然,只有在适当地设置UserAd模型中的关联时,它才会起作用。