DEV Community

kirthinandi
kirthinandi

Posted on

Active Record: Seed Data

Image description

What is Seed Data?

Seed data is sample data. When creating or working with an application that requires a database, it is ideal to also create sample data that can be used to test out the application and its function in the future. In Active Record, the process of making the sample data to be inputted in the database is also known as "seeding the database".

Why Should We Use Seed Data?

You may be wondering what the purpose of seed data is when we can already create data in the console. It is encouraged to create a seed data Ruby file so that it is easier to share data and the instructions on how to create this data with other developers. This file can also be useful if there are instances where the development database gets deleted.

How Can We Create Seed Data?

There are a few simple steps in which we can produce seed data. First, we need to create a Ruby file called seeds.rb in the db folder. Next, using Active Record Methods, we can create new data. In the following example, we will be adding new food items to go in a foods table that has already been created. When creating the data, we need to include the table's column names that we want to provide information in. In the example below, the first food item's name we are adding is the Mexican Pizza. To add it to the name column in our food table, we need to add name: "Mexican Pizza" in the create method.

#db/seeds.rb

Food.create(name: "Mexican Pizza", price: "$4.49", calories: "540 cal");
Food.create(name: "Grilled Cheese Burrito", price: "$4.29", calories: "720 cal");
Food.create(name: "Nachos BellGrande", price: "$4.69", calories: "730 cal");
Enter fullscreen mode Exit fullscreen mode

After writing the code necessary to generate the data, we can run bundle exec rake db:seed in the terminal. Since there won't be any output to the terminal after running this command, we can enter the terminal's console by running rake console in the terminal. To have all the new food items that have been added to the table returned, we can type Food.all in the console. To see the first food item, we can type Food.first in the console. To see the second food item, we can type Food.second and so on. To see a specific attribute of a specific item, we can type Food.first.name or Food.second.price.

How To Create More Data?

Oh no! We forgot to add important data! To make/add new data after creating the original data, we can use the Active Record methods similarly to how we did in the example above in the seeds.rb file.

#db/seeds.rb

Food.create(name: "Mexican Pizza", price: "$4.49", calories: "540 cal");
Food.create(name: "Grilled Cheese Burrito", price: "$4.29", calories: "720 cal");
Food.create(name: "Nachos BellGrande", price: "$4.69", calories: "730 cal");

#new data 
Food.create(name: "Chicken Quesadilla", price: "$4.39", calories: "520 cal");
Food.create(name: "MTN DEW Baja Blast", price: "$2.19", calories: "280 cal");

Enter fullscreen mode Exit fullscreen mode

After adding the data to the seeds.rb file, we will need to exit the console using the control + c keys on our keyboard and running the seeds.rb file again using the bundle exec rake db:seed command in the terminal. Then, we can enter the console again to test out our new data by typing rake console in the terminal. After entering the console, we can type Food.all to see all the items listed including the new items we added. If you want to remove the data from all the existing tables and re-run the seed file, you can run the command, bundle exec rake db:seed:replant in the terminal after exiting the console. Use this command with caution because it will delete all the data currently existing.

Conclusion

Now that you understand the importance of using seed data and how to create it, you are ready to go ahead and try producing sample data on your own.

Latest comments (1)

Collapse
 
dharshann profile image
Dharshan Bharathuru • Edited
require 'faker'

(1..100).each do |_id|
  Food.create!(name:    Faker::Food.dish, 
              price:    Faker::Number.decimal(l_digits: 2),
              calories: Faker::Number.decimal_part(digits: 2))
end
Enter fullscreen mode Exit fullscreen mode