Seeding a database using the Rails command line


Ruby on Rails has very good tools to seed a database, and thanks to the community efforts, there are several gems to make that task easier. Besides seeding the database, we have useful tools to check the database and ways to organize better important data seeds.

Creating a sample application

Let’s start typing rails in a Rails application directory to check out all the available commands:

We will see the common commands, and below some additional ones. We are going to use some of them. In fact you used one: new, used to bootstrap a new application.

Creating a model

Next I’m going to generate a new model. Documentation is very good, because typing rails generate (or the rails g shortcut) displays all available generators.

Here I’m setting the title and director as string (default type if not specified), storyline as text, and watched_on as date (when setting dates, not datetimes, a convention is to append on to the field).

Rails will generate a migration for us adapted to the default database, which is SQLite. Migrations are saved in db/migrations. Let’s see how it looks like!

Very straightforward. The only remarkable thing is the timestamps statement: it will generate the created_at and updated_at fields automatically, very handy. Let’s run it.

So now Rails has actually created the table. Just in case you did something wrong, you can always rollback:

This commands accepts an optional step parameter to go back as many migrations as needed.

So now that it’s created, let’s see how the schema looks like in db/schema.rb:

Cool! This will contain the entire database schema as we run more migrations.

Rake commands

As a side note: how can you know about rake commands? By using the -T parameter you can see a list of them:

You can even limit by namespace, such as db:

Creating some seeds

Let’s go for the interesting part of this article! Open the db/seeds.rb file, and paste this:

First we destroy all movies to have a clean state, and add 3 movies passing an array to the create method. The seeds file uses Rails ActiveSupport, so we can use those handy Ruby day.ago statements to define dates.

At the end I give some feedback about the total movies created. Let’s run it!

You can run this command as many times as you need thanks to the destroy statement in the first line.

To check them you can use the Rails runner:

Using a custom rake task to seed important data

All our seeds are considered development data, not important data for production use. So, don’t seed like we did in production! Especially because the first step deletes all movies!

To seed important data is better to create a custom rake task. Let’s create one to add genres:

This creates a movies rake file in the lib directory containing the seed_genres task (you could add more from the command line). It looks like this:

It’s listed in the rake commands list:

Time to invoke it!

Loading seeds using the console

The console if very useful to play around with your data. Let’s fire it up:

Did you know that you can load your seeds from inside? You don’t need to run rakeanymore! Try this:

Playing with data using the console sandbox

Sometimes you will need to run destructive commands on your development or production environment, but without affecting your real data. It’s something like a safe mode where you can do whatever you like and then rollback to the previous state.

To access this mode just run rails c --sandbox. Then you can do something like this:

  • Movie.update_all(title: 'Foo')  to update all movies titles.
  • Movie.first.title  will display Foo.

If you exit the console, and run it again to check the first movie title, it will have the original title.

This is very useful for production debugging, like when a user says that updating the profile name he gets some weird error. We could try to reproduce that error directly using the sandbox mode, without affecting the application.

Loading more seeds using Faker

If you need, say, 100 movies, you can replace your seeds file with this:

But that doesn’t look realistic at all:

Time to use Faker, a gem that generates random values. Add it into the development group inside your Gemfile:

Run bundle install and use these seeds:

Check one out again:

Much better!


Seeding your application when developing it is very important because you will feel it like it has real data. That’s interesting to see how it looks like because data will have random length.

Also, knowing the available tools to work with seeds makes us feel more comfortable and more productive, so it’s worth it to invest some time to learn and practice.

Do you use any other interesting seeding technique or tool worth trying?

Did you like it? Please share it:

Get my ebook for free

10 ideas that helped me become a better developer (and may help you too)

Subscribe to my mailing list and get my ebook on 10 ideas that helped me become a better developer.

About Me

David Morales

David Morales

I'm David Morales, a computer engineer from Barcelona, working on projects using Ruby on Rails and training on web technologies.

Learn More