Skip to content

Instantly share code, notes, and snippets.

@bbugh
Created January 28, 2013 18:14
Show Gist options
  • Save bbugh/4657749 to your computer and use it in GitHub Desktop.
Save bbugh/4657749 to your computer and use it in GitHub Desktop.
Using traits in FactoryGirl to do things like add tags to models, or unassociated data that multiple models would share.

Using traits in FactoryGirl for things like tags on other models, and so forth.

FactoryGirl.define do
  factory :post do
    title 'New post'
  end

  trait :with_comments do
    after :create do |post|
      FactoryGirl.create_list :comment, 3, :post => post
    end
  end 
end

FactoryGirl.create :post, :with_comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment