0

我有一个广告工厂,但如何将其分配给 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 }
4

1 回答 1

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

user.ads #=> array of ads

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

于 2012-08-13T01:32:11.037 回答