# How to create associations with Factory Girl?

**URL:** <https://www.ruby-forum.com/t/how-to-create-associations-with-factory-girl/166368>\
**Category:** Rails\
**Created:** [April 24, 2009, 10:54am UTC](https://www.ruby-forum.com/t/how-to-create-associations-with-factory-girl/166368 "2009-04-24T10:54:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [April 24, 2009, 10:54am UTC](https://www.ruby-forum.com/t/how-to-create-associations-with-factory-girl/166368/1 "2009-04-24T10:54:40Z")

</div>

I watched the RailsCasts episode on factory\_girl, and I’m trying to  
get out of fixtures.

- Does anybody know an open source project that uses many of the  
factory\_girls features?

I came up with a pseudo code for possibly showing some association  
features I don’t understand yet.

Here’s the code. It’s blog posts with comments plus tagging:  
############################ CODE STARTS  
create\_table “posts”, :force =\> true do |t|  
t.string “title”  
t.text “content”  
end

create\_table “comments”, :force =\> true do |t|  
t.ingeter “post\_id”  
t.string “content”  
end  
create\_table “tagging”, :force =\> true do |t|  
t.integer “tag\_id”  
t.integer “post\_id”  
end

create\_table “tags”, :force =\> true do |t|  
t.string “name”  
end

########################################  
class Post  
has\_many :comments  
has\_many :tags  
has\_many :tags, :through =\> :tagging  
end

class Tags  
has\_many :posts  
has\_many :posts, :through =\> :tagging  
end

class Tagging  
belongs\_to :post  
belongs\_to :tag  
end  
#########################################

Factory.define :post do |p|  
p.title ‘On Girls’  
p.content ‘bar foo’

# Do I need p.association? But how can I connect

end

Factory.define :tag do |t|  
t.name ‘Ruby’  
end

Factory.define :tagging do |tagging|

# What am I going to do here?

end  
Factory.define :comment do |c|  
c.conent ‘foo bar’  
c.association :post, :factory =\> :post  
end  
#################################### END OF CODE  
I tried in many ways, but I’m not getting how to deal with  
associations.  
Can anybody help filling in code?

John Hunt
