开发者

How does the many-to-many association work in FactoryGirl? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to make has_many :through association with fixtures?

I tried use FactoryGirl to build my test data. But I don't know how to build many-to-many associations.

Finally, I google-copy-paste a snippet:

factory :tagging do 
  question { |a| a.association(:question) } 
  tag { |a| a.association(:tag) } 
end

(question has_many tags through taggings, vice versa)

It works well. But what's this strange syntax? What's happen when I put a block after the attribute name? The officia开发者_如何学JAVAl readme didn't tell me.

Could someone help?


As you described tagging model has at least two columns: question_id and tag_id. You can set them manually, but you should create new objects before that. FactoryGirl does it for you through its association feature. It creates new object (you specify factory for that object as argument in association function), gets its id and sets it to appropriate field.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜