I have already built some elaborate FactoryGirl factory definitions for testing a Rails project, and for this purpose they are working well.
I'd now like to use the same definitions with a script (Ruby, Rake, whatever...) to populate the Rails development database with a large collection of valid, correctly associated records.
I'm sure this is a common task but cannot find a useful reference.
What is best practice? How to proceed?
Say the factories are in spec/factories.rb
. What next? Thanks.
Update
Still struggling. Tried this as a rake
task.
require 'factory_girl'
require 'spec/factories'
namespace :db do
desc "Fill database with trial data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
50.times do |n|
# Make a consistent set of related records.
team = FactoryGirl.create(:team, :completed)
team.members << FactoryGirl.create(:member)
FactoryGirl.create(:design, :team => team)
end
end
end
Alas, this can't find my factories.rb
:
rake aborted!
cannot load such file -- spec/factories
Everywhere else a project root-relative require path works fine. What is the secret handshake?
gem factory_girl
in thegroup test
only ? If so, it has to be in development also in order to use it like you want to. – Hellibundle install
ed it. I'm sorry I don't know whatgroup test
means. Thanks for the help. – Galitea