collimarco
collimarco

Reputation: 35400

Populate database with sample (fake) data

What is the best way to populate the development database with sample data?

FactoryGirl seems to be useful for tests, but not suitable for development. Populator is not updated to Rails 3.

Should I use something like Faker or Forgery in a rake task?

Upvotes: 2

Views: 3574

Answers (3)

zack999
zack999

Reputation: 111

Use faker gem in your Gemfile, bundle.. and... put this in your seeds.rb

100.times do
  Post.create({
    title: Faker::Lorem.sentence(3),
    body: Faker::Lorem.paragraph,
    author_name: Faker::Name.name
    })
end

$rake db:seed

Upvotes: -1

Lee Smith
Lee Smith

Reputation: 468

I'm using ffaker and populator gems in a rake task to create fake data in a Rails 3 project. Here's an example:

desc 'Create some fake tickets'
  task :tickets => :environment do
  Ticket.populate NUM_TICKETS do |t|
    t.title = Faker::Lorem.sentence(word_count=15)
    t.details = Faker::HipsterIpsum.paragraphs(sentence_count=3)
    t.group_id = rand(6)+1 # random group_id [1..6]
    t.status_id = 1
    t.priority_id = rand(3)+1 # random priority_id [1..3]
    t.contact_id = rand(NUM_CONTACTS)+1 # random contact_id [1..NUM_CONTACTS]
    t.creator_id = rand(6)+2 # random created_by [2..7]
    t.created_at = CREATION_PERIOD.sample
  end
end

Full rake task here: https://github.com/leesmith/ticket_mule/blob/edge/lib/tasks/faker.rake

Upvotes: 6

Anthony Alberto
Anthony Alberto

Reputation: 10395

This looks promising : https://github.com/paulelliott/fabrication

Upvotes: 2

Related Questions