Define seed data to allow you to populate your tables with information that is essential to an application’s operation or to save time in creating standard content.
- [Instructor] In this movie, we'll learn how to use seed data in your Rails applications. Migrations can modify database data as well as the database structure. This is handy when the data is important to the transition from one state to another. For example, when you're adding a categories table, it's common to also populate that table with an initial set of categories. In these cases, you can put the data changes into a regular migration. But what about when you want to have a starting set of data before any migrations have run? You could put these data changes into the very first migration, but in the last movie we saw how to load the database schema in order to avoid running those migrations.
It would be much handier if we could put that initial data somewhere else so that we could load it in. Rails supports a concept of seed data. Seed data is an initial set of data for an application and it's usually the minimum data required to make the application work properly or to put it in a good state so we can begin developing. It's useful when we want to initialize a new application. So we'd load in the schema, we have the schema, and then we load in that initial set of seed data. It's also useful when we're running Rails tests and we want our test environment to have some essential information available to it.
An example of seed data might be to pre-populate that categories table with an initial set of product categories. Another classic example would be to create an initial admin user who has access to the admin features of the site. That way when we start developing, we have an initial admin at the beginning or our test environment has that admin user that it can work with to test out the admin features of the site. The Rails convention is to put this seed data in a single file inside the db directory, and it's called seeds.rb. As you can guess from the line ending, that's a file with code in it written in Ruby, so we can write any Ruby code in there.
It's really up to us what we want to put in there. And we also have access to the Rails framework and our application code, so we can make use of our models and helpers as we're writing that seed data. And then once we've got everything in there correctly, we can run the contents of that file, and execute all of that Ruby code with rails db:seed. This works well when we pair it with create and schema:load so that we can call rails db:create when we create the database defined in the database .YML file, rails db:schema:load will load in the most recent schema, and rails db:seed will put the initial starting setup data that we need into the application.
Now we have the database, the structure, and the data, and we're ready to start developing. Working with seed data is very simple. All we have to do is go into the seeds.rb file and then add any code that we want here. That code could do absolutely anything. Now the main thing we probably want to do is we want to put entries into the database. But it could find records, it could look for other values, all sorts of things like that could happen. But for now, let's keep it simple. I'm just going to create a new product. If you remember back over here inside my schema file, I had already created a table for products.
So I've got various columns on there. Now I've got seed data that's going to populate that with different things. And I'm using product, which is my model, I'm calling create on it so it'll save it to the database right away, and I notice I've got an extra single quote there, name Couch, description Modern blue couch, inventory 200, price $100, restocked_at and I'm using Time.now, a bit of Ruby, qty_sold equals, and ref_num, and I'm giving it a reference number. So that's it. I've just got Ruby code that does that. I can copy that, and I can make second one, and let's make this one a Chair.
This one is going to be a, we'll call it Brown chair. Inventory, we'll make it 100, price, let's make it $40, restocked_at, we'll make it 1.day.ago, and qty_sold is still zero, reference number we'll make this two. So now we're going to create two products. We could just keep going. We could have as many as we want in there. It's just simply Ruby code, that's all it is. And then we come back over here, and when we want to run that code, we just simply go into the root of our project and type rails db:seed, and wait just a moment.
We'll go to that file, it'll run our code. There it goes. We don't get any confirmation of it, but if we go into mysql and log in as rails_user with password, and I'm going into demo_project_development. And there I am. Now let's say SELECT * FROM products, and there you go. You can see I populated it with those two values. This gives you a nice, quick way from the command line to get your application into a good state to start developing.
- Migrating databases
- Creating, updating, and deleting records
- Tracking changes to objects and attributes
- Writing queries
- Delegating attributes
- Working with associations