Using Seed Data with Active Record
Published: 2023-01-16 00:37:09

What is Seed Data?
Seed data is just sample data. When creating or working with an application that requires a database, creating sample data is ideal because it can be used to test out the application and its function. In Active Record, along with other ORM's,  the process of making the sample data and inputting it in the database is also known as "seeding" the database.

Why Should We Use Seed Data?
It is encouraged to create a seed data Ruby file so that, instead of sharing the actual database, we share the instructions on how to create data in the database. This makes it easier for developers working on the same application.  Especially if there is instance where the development database gets deleted.

How Can We Create Seed Data?
There are a few steps in which we can create seed data. First, in the db folder, we need to create a Ruby file called seeds.rb. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ple4vj0189h96u49d9iy.png)

Next, using Active Record Methods, we can create new data. In the example from my Phase 3 project here at Flatiron, I added new Pets to go into a pets table. When creating the data, we need to include the table's column names along with the pet's information.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2xiuefff8ir9ipb81vt.png)


Next, run bundle exec rake db:seed in the terminal. By adding a "puts" of "Seeding data" at the beginning of the file, and adding an additional "puts" of "Done seeding!" at the end of the file, we know that the seeding should be completed.  Next, to verify that the information was seeded,  we want to enter the terminal's console by running "bundle exec rake console" in the terminal. To have all the new pets that have been added to the table returned, we can type Pet.all in the console. To see the first pet, we can type Pet.first in the console.

Adding Additional Data
To 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. Just add the data as before, including the table's column names along with the pet information.

After adding the data to the seeds.rb file, exit the console by typing "exit" and run the seeds.rb file again using the bundle exec rake db:seed command in the terminal. Then enter the console again to test out our new data by typing "rake console" in the terminal. After entering the console, we can type Pet.all to see all the pets listed including the new pets 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. Be careful when replanting because it will delete all of the current data.

Using the "Faker gem"
Faker is a gem used for generating fake data such as names, addresses, and phone numbers.  This is a useful gem when you need realistic sample data but the actual content of the data is not important.  More information and instructions on how to install the gem is found here: https://github.com/faker-ruby/faker

Now we should have plenty of seed data to work with. This makes an easy way for ourselves or other developers to populate and work with the database any time we need to do so.

Resources:
https://guides.rubyonrails.org/v5.1/active_record_migrations.html
https://www.rubydoc.info/gems/faker/