Yesterday, the Rails team released 2.3.4, which includes standardized
way for loading seed data into your application so that you didnât have
to clutter your database migrations.
I noticed a few comments on some blogs where people were asking how to
use this new feature, so here is a quick runthrough a few ways that you
can use it.
Populating Seed Data Approaches
The db/seeds.rb
file is your playground. Weâve been evolving our seed
file on a new project and itâs been great at allowing us to populate a
really large data. Here are a few approaches that weâve taken to
diversify our data so that when weâre working on UI, we can have some
diversified content.
Basic example
Any code that add to db/seeds.rb
is going to executed when you run
rake db:seed
. You can do something as simple as:
- db/seeds.rb
Article.create(:title => âMy article titleâ, :body => âLorem ipsum
dolor sit amet, consectetur adipisicing elitâ)\
Just create database records like you would in your Rails application or
in script/console
. Simple enough, right? Letâs play with a few other
approaches that weâve begun to use.
Use the names of real people
Weâre using the Octopi gem to
connect to github, collect all the names of people that follow me there,
and using their names to seed our development database.
\@robby_on_github = Octopi::User.find(ârobbyrussellâ)
- add a bunch of semi-real users
\@robby_on_github.followers.each do |follower|
github_person = Octopi::User.find(follower)
next if github_person.name.nil?
# split their name in half⌠good enough (like the goonies)
first_name = github_person.name.split(â â)[0]
last_name = github_person.name.split(â â)[1]
new_person = Person.create(:first_name => first_name, :last_name =>
last_name, :email => Faker::Internet.email,
:password => âsecretâ, :password_confirmation => âsecretâ,
:github_username => follower, :website_url => github_person.blog)
# âŚ
end\
We do this with a few sources (twitter, github, etc..) to pull in the
names of real people. If you want to be part of my seed data, you might
consider following me on Github. ;-)
Use Faker for Fake data
You may have noticed in the previous code sample, that I used Faker in
that code. We are using this a bunch in our seed data file. With Faker,
you can generate a ton of fake data really easy.
person.links.create(:title => Faker::Lorem.words(rand(7)+1).join(â â).capitalize,
{lang=ârubyâ}
:url => âhttp://#{Faker::Internet.domain_name}/â,
{lang=ârubyâ}
:description => Faker::Lorem.sentences(rand(4)+1).join(â â))
{lang=ârubyâ}\
We might toss something like that into a method so that we can do the
following:
\@people = Person.find(:all)
500.times do
generate_link_for(@people.sort_by{rand}[0])
end\
âŚand weâll get 500 links added randomly across all of the people we
added to our system. You can get fairly creative here.
For example, we might even wanted random amounts of comments added to
our links.
def generate_link_for(person)
link = person.links.create(:title =>
Faker::Lorem.words(rand(7)+1).join(â â).capitalize,
:url => âhttp://#{Faker::Internet.domain_name}/â,
:description => Faker::Lorem.sentences(rand(4)+1).join(â â))
# letâs randomly add some commentsâŚ
if link.valid?
rand(5).times do
link.comments.create(:person_id => \@people.sort_by{rand}[0].id,
:body => Faker::Lorem.paragraph(rand(3)+1))
end
end
end\
Itâs not beautiful, but it gets the job done. It makes navigating around
the application really easy so that we arenât having to constantly input
new data all the time. As mentioned, it really helps when weâre working
on the UI.
Your ideas?
Weâre trying a handful of various approaches to seed our database. If
you have some fun ways of populating your development database with
data, weâd love to hear about it.