How to create associations with Factory Girl?

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs