# Using Factory Girl with has many relationship

**URL:** <https://www.ruby-forum.com/t/using-factory-girl-with-has-many-relationship/194378>\
**Category:** Rails\
**Created:** [September 3, 2010, 12:07pm UTC](https://www.ruby-forum.com/t/using-factory-girl-with-has-many-relationship/194378 "2010-09-03T12:07:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@mosh](https://www.ruby-forum.com/u/mosh)\
**Post date:** [September 3, 2010, 12:07pm UTC](https://www.ruby-forum.com/t/using-factory-girl-with-has-many-relationship/194378/1 "2010-09-03T12:07:53Z")

</div>

Hello,

I have this kind of relation:

class Article \< ActiveRecord::Base  
has\_many :comments  
end

class Comment \< ActiveRecord::Base  
belongs\_to :article  
attr\_protected :article\_id  
end

The default scenario inside controllers looks like:

@article = Article.create(:title =\> “foobar”)  
@comment = @article.comments.create(:content =\> “w00t”)

I had tried to write those factories:

Factory.define :article do |f|  
f.title “Hello, world”  
end

Factory.define :comment do |f|  
f.content “Awesome!”  
f.association :article  
end

But my syntax is not correct about the association. It’s a little bit  
tricky because of the comment’s article\_id protected attribute. So I  
think this should be better if I declare the association inside the  
article factory, but I don’t see how to process.

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![mosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@mosh](https://www.ruby-forum.com/u/mosh)\
**Post date:** [September 3, 2010, 2:40pm UTC](https://www.ruby-forum.com/t/using-factory-girl-with-has-many-relationship/194378/2 "2010-09-03T14:40:24Z")

</div>

On Fri, Sep 3, 2010 at 6:07 AM, Satsou Sa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I had tried to write those factories:
> 
> Factory.define :article do |f|  
> f.title “Hello, world”  
> end
> 
> Factory.define :comment do |f|  
> f.content “Awesome!”  
> f.association :article  
> end

I’m still pretty newb, so I could be way off here, but I think you  
need to define the factory that represents the association?

Factory.define :comment do |f|  
f.content “Awesome!”  
f.association :article, :factory =\> :article  
end

---

<div class="post-metadata">

**Author:** ![mosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@mosh](https://www.ruby-forum.com/u/mosh)\
**Post date:** [September 3, 2010, 2:43pm UTC](https://www.ruby-forum.com/t/using-factory-girl-with-has-many-relationship/194378/3 "2010-09-03T14:43:20Z")

</div>

On Fri, Sep 3, 2010 at 8:38 AM, Rick R [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > f.association :article  
> > end
> 
> I’m still pretty newb, so I could be way off here, but I think you  
> need to define the factory that represents the association?
> 
> Factory.define :comment do |f|  
> f.content “Awesome!”  
> f.association :article, :factory =\> :article  
> end

It’s also a bit confusing because your naming your factories the same  
as the property name. It might be more clear if you set them with more  
‘real’ names like

Factory.define :hello\_world\_article, :class =\> Article do |f|  
f.title “Hello, world”  
end

Factory.define :awesome\_comment, :class =\> Comment do |f|  
f.content “Awesome!”  
f.association :article, :factory =\> :hello\_world\_article  
end

–  
Rick R

---

<div class="post-metadata">

**Author:** ![mosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@mosh](https://www.ruby-forum.com/u/mosh)\
**Post date:** [September 6, 2010, 9:09am UTC](https://www.ruby-forum.com/t/using-factory-girl-with-has-many-relationship/194378/4 "2010-09-06T09:09:40Z")

</div>

Many thanks for your help. Very appreciated.

Happy RoR3 developments!

---

<div class="post-metadata">

**Author:** ![mosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@mosh](https://www.ruby-forum.com/u/mosh)\
**Post date:** [September 3, 2010, 2:58pm UTC](https://www.ruby-forum.com/t/using-factory-girl-with-has-many-relationship/194378/5 "2010-09-03T14:58:34Z")

</div>

Try this

Factory.define :article do |f|  
f.title “Hello, world”  
f.comments {|comments| [comments.association(::comment)]}  
end

Factory.define :comment do |f|  
f.content “Awesome!”  
end
