BookOfGreg
BookOfGreg

Reputation: 3716

Rails How to use FactoryGirl in Seeds.rb?

I want to have the Seeds.rb file run a method in a file from the Rails.root.join('spec') directory which will fill the database with data generated by FactoryGirl. Lets call this file "helpers.rb" with the method "seed_data"

Edit: require Rails.root.join('spec','helpers.rb') links the file. source

This method I want to use in a before(:all) do seed_data end to seed the test database and rake db:seed for development/production databases with the same effect.

Upvotes: 3

Views: 2550

Answers (1)

BookOfGreg
BookOfGreg

Reputation: 3716

Seeds.rb

require Rails.root.join('spec','helpers.rb')
require 'rubygems'           #so it can load gems
require 'factory_girl_rails' #so it can run in development
seed_data

spec/helpers.rb

def seed_data
  Factory.create(:user)
end

Upvotes: 3

Related Questions