Skip to content

Instantly share code, notes, and snippets.

@jgv
Created January 25, 2013 22:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgv/4638318 to your computer and use it in GitHub Desktop.
Save jgv/4638318 to your computer and use it in GitHub Desktop.
Populate a database with data from Factory Girl.
FactoryGirl.define do
factory :picture do
image { File.open(File.join(Rails.root, 'spec', 'support', 'pictures', 'spec.png')) }
end
end
require 'factory_girl'
namespace :db do
desc "Populate the dev database with some sample data"
task :populate, [:count] => [:environment] do |t, args|
args.with_defaults(:count => 10)
puts "Resetting the database"
Rake::Task['db:reset'].invoke
puts "Creating #{args[:count]} users"
new_users = FactoryGirl.create_list(:user, args[:count].to_i)
puts "Creating a product for each user"
new_users.each_with_index do |u|
FactoryGirl.create(:product, :user_id => u.id)
end
puts "Done!"
end
end
FactoryGirl.define do
factory :product do
name "a thing"
price "420"
description "blah blah blah"
slug "a-thing"
before(:create) {|p| FactoryGirl.create(:picture, :attachable => p) }
user
end
end
FactoryGirl.define do
factory :user do
sequence(:email) {|n| "person-#{n}@okfoc.us" }
firstname "tester"
sequence(:lastname) {|n| "cool #{n}" }
password "password"
sequence(:username) {|n| "foo-#{n}" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment