Seeding the Database

seeds.rb

So now we've used Rails' conveniences to recreate our database tables, but they're empty again. We could use the CSV files from before to repopulate them, but instead let's define a db/seeds.rb file to demonstrate how we can seed our database with test data programmatically using Ruby and our models:

### db/seeds.rb ###

puts "Seeding"

print "  posts "

posts = [
  {
    title:     'Blog 1',
    body:      'Lorem ipsum dolor sit amet.',
    author:    'Kevin',
  },
  {
    title:     'Blog 2',
    body:      'Lorem ipsum dolor sit amet.',
    author:    'Chris',
  },
  {
    title:     'Blog 3',
    body:      'Lorem ipsum dolor sit amet.',
    author:    'Brad',
  }
]

time = DateTime.parse '2016-5-07'

posts.each do |post_hash|
  Post.create post_hash.merge(created_at: time, updated_at: time)

  time += 1.day

  print '.'
end


print "\n  comments "

comment_authors = %w[Matz DHH tenderlove]

Post.all.each do |post|
  3.times do
    post.comments.create \
      body:      'Lorem ipsum dolor sit amet.',
      author:     comment_authors.sample,
      created_at: time,
      updated_at: time

    time += 1.day

    print '.'
  end
end
puts

This creates our three posts from before and then creates three comments per post by a random author.

Now, if you have actual data you want to use, a CSV may be a good choice, but in the case that you just want to generate some data to play with, seeding this way may be a better option.

You can use this file to seed your DB with the following command:

$ bundle exec rake db:seed
Seeding
  posts ...
  comments .........

Let's hop into Rails console real quick to make sure that the seeding process worked as intended.

$ bundle exec rails c
irb(main):001:0> Post.all
  Post Load (4.2ms)  SELECT "posts".* FROM "posts"
=> #<ActiveRecord::Relation [#<Post id: 1, title: "Blog 1", body: "Lorem ipsum dolor sit amet.", author: "Kevin", created_at: "2016-05-07 00:00:00", updated_at: "2016-05-07 00:00:00">,
#<Post id: 2, title: "Blog 2", body: "Lorem ipsum dolor sit amet.", author: "Chris", created_at: "2016-05-08 00:00:00", updated_at: "2016-05-08 00:00:00">,
#<Post id: 3, title: "Blog 3", body: "Lorem ipsum dolor sit amet.", author: "R2D2", created_at: "2016-05-09 00:00:00", updated_at: "2016-05-09 00:00:00">]>
irb(main):002:0> Comment.all
  Comment Load (1.3ms)  SELECT "comments".* FROM "comments"
=> #<ActiveRecord::Relation [#<Comment id: 1, body: "Lorem ipsum dolor sit amet.", author: "DHH", post_id: 1, created_at: "2016-05-10 00:00:00", updated_at: "2016-05-10 00:00:00">,
#<Comment id: 2, body: "Lorem ipsum dolor sit amet.", author: "Matz", post_id: 1, created_at: "2016-05-11 00:00:00", updated_at: "2016-05-11 00:00:00">,
#<Comment id: 3, body: "Lorem ipsum dolor sit amet.", author: "Matz", post_id: 1, created_at: "2016-05-12 00:00:00", updated_at: "2016-05-12 00:00:00">,
#<Comment id: 4, body: "Lorem ipsum dolor sit amet.", author: "DHH", post_id: 2, created_at: "2016-05-13 00:00:00", updated_at: "2016-05-13 00:00:00">,
#<Comment id: 5, body: "Lorem ipsum dolor sit amet.", author: "tenderlove", post_id: 2, created_at: "2016-05-14 00:00:00", updated_at: "2016-05-14 00:00:00">,
#<Comment id: 6, body: "Lorem ipsum dolor sit amet.", author: "tenderlove", post_id: 2, created_at: "2016-05-15 00:00:00", updated_at: "2016-05-15 00:00:00">,
#<Comment id: 7, body: "Lorem ipsum dolor sit amet.", author: "Matz", post_id: 3, created_at: "2016-05-16 00:00:00", updated_at: "2016-05-16 00:00:00">,
#<Comment id: 8, body: "Lorem ipsum dolor sit amet.", author: "DHH", post_id: 3, created_at: "2016-05-17 00:00:00", updated_at: "2016-05-17 00:00:00">,
#<Comment id: 9, body: "Lorem ipsum dolor sit amet.", author: "Matz", post_id: 3, created_at: "2016-05-18 00:00:00", updated_at: "2016-05-18 00:00:00">]>

Great, it seems we have 3 posts, and 3 comments for each of those posts, just as we specified in our seeds.rb file.