Created
January 25, 2013 22:03
-
-
Save jgv/4638318 to your computer and use it in GitHub Desktop.
Populate a database with data from Factory Girl.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FactoryGirl.define do | |
factory :picture do | |
image { File.open(File.join(Rails.root, 'spec', 'support', 'pictures', 'spec.png')) } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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