Seeding a Database in Ruby on Rails
Ruby on Rails has excellent tools for seeding a database, and thanks to the work of the community, some gems make this task easier.
Apart from seeding a database, we have helpful tools to check the database and better organize the data seeds.
Creating a sample application
Let’s start by creating a new application:
Creating a model
Next, generate a new model. If you are curious, by typing rails generate
(or the rails g
shortcut), you will see all the available generators.
Here you are setting the title
and director
as strings (the default type if not specified), storyline
as text, and watched_on
as date (when setting dates, not datetimes, the convention is to append on
to the name).
Rails will generate a migration for you adapted to the default database, which is SQLite. Migrations are saved in db/migrate Let’s see how it looks like:
As you can see, Rails adds the version you are using in square brackets at the end of the parent class.
The timestamps statement will generate the created_at
and updated_at
fields automatically.
Let’s run it:
Now Rails has actually created the table. Just in case you did something wrong, you can always rollback:
This command accepts an optional step
parameter to go back as many migrations as needed. For example, if you want to undo 2 migrations, you can do it like this:
Let’s see how the schema looks like after running the migration:
This file will contain the entire database schema as we run more migrations.
Rails commands
By using the -T
parameter you can see a list of the available Rails commands:
You can even filter by namespace, such as db
:
Creating some seeds
Let’s get to the interesting part of this article. Open the seeds file, and paste this:
First, you destroy all movies to have a clean state and add three movies passing an array to the create
method. Those handy Ruby X.ago
statements to define dates are provided by the Active Support Core Extensions.
In the end, there’s some feedback about the total movies created. Let’s run it!
You can execute this command as many times as you need, since existing records are deleted thanks to the first line containing the destroy
statement.
To check them, you can use rails runner
:
Using a custom Rails task to seed actual data
All your seeds are supposed to be development data, not actual data for production use. So, don’t seed in production the way you just did! Mainly because the first step deletes all movies!
To seed actual data, it is best to create a custom Rails task. Let’s generate one to add genres.
First generate the model and then migrate the database. Next, create the task.
This command creates a movies.rake file in the lib/tasks directory containing the seed_genres
task.
Paste this code into the new rake file:
It’s now listed in the Rails commands list:
Time to run it!
Loading seeds using the console
The console is handy for playing with your data. Let’s open it:
Did you know that you can load and access your seeds from the inside? Try this:
Playing with data using the console sandbox
Sometimes you will need to run destructive commands on real data in your development or production environment without making the changes permanent. It’s kind of like a safe mode where you can do whatever you want and then go to a previous state.
This mode is called sandbox, and you can access it with the command bin/rails c --sandbox
This technique is handy for debugging a real database, such as when a user says they are trying to update their profile name and see a weird error. You could reproduce that error directly using the sandbox mode without affecting the actual data.
Loading more seeds using Faker
If you need, for example, 100 movies, you can replace your seeds file with this:
Now run the seed
task:
But the result does not look realistic at all:
Time to use Faker, a gem that generates random fake values. Add it into the development group in your Gemfile:
Run bundle install
and replace your seeds file with this:
Check it out again:
Much better!
Test your knowledge
-
What is the primary purpose of the db/seeds.rb file in a Ruby on Rails application?
-
How do you run the seeds file to populate the database with seed data?
-
Why is it not recommended to seed production data using the db/seeds.rb file?
-
What is the purpose of creating a custom Rails task for seeding actual data?
-
What is the advantage of using the Rails console in sandbox mode?
Conclusion
Seeding the database while developing the application is essential, as it will have the feeling of working with actual data.
Also, knowing the tools available for working with seeds is good for your productiveness, so it is worth investing time in learning them.
If you are new to Ruby on Rails, I would like to recommend a course I created on LinkedIn Learning where I explain the basics of the framework from a practical point of view. You will learn the basics of Active Record, the Scaffold generator, the asset pipeline, ERB code, working with images with Active Storage, reactive code with Hotwire, and much more.