Feed the Database
Discover how to efficiently populate your Rails API database by using the db/seeds.rb file and the faker gem. Learn to create multiple users and associate products with them, automating dummy data creation for testing and development.
We'll cover the following...
Let’s fill the database with dummy data before continuing with more code. With the file db/seeds.rb, Rails gives us a way to easily and quickly provide default values for a new installation. It’s a simple Ruby file that offers full access to all classes and methods of the application so that we don’t need to enter everything manually with the Rails console. We can use the db/seeds.rb with the rake db:seed command.
The below code demonstrates how seed.rb creates a user.
ruby-2.6.3
Click the “Run” button to open a terminal and create the user ...