Behold the Mighty Seed!
In this post, I’ll talk about a feature of Rails that sometimes gets passed over in beginner-level Rails courses because it is not at all necessary for building web applications, but nevertheless can be very useful. This feature is the ability to add seed data to the database. The best way to illustrate this is by example. During my web development training at [theFirehoseProject] (http://thefirehoseproject.com) I built a restaurant review app called [Nomster] (http://nomster-yury-voloshin.herokuapp.com). When the features needed to add restaurants were complete, I added several restaurants to the database by typing in their information to make sure the places are correctly styled. A few more places needed to be typed in in order to test the pagination feature. But then, a typo during a database migration led to my database becoming inaccessible. The problem had to be fixed by dropping the database and creating a new one. While I appreciated the learning experience that this incident brought me, I did not appreciate having to type in the information for all the restaurants one more time. At that moment, my life would’ve been easier if I could just load a file where all the restaurant information has been pre-loaded.
It turns out that such a file does exist. It is the db/seeds.rb file that is automatically generated by Rails. This is the place where we can enter test data to be used by our application. The data needs to be typed in only once and can be loaded into the app as many times as necessary. This can be very useful for those times when the database needs to be re-created. When working on a development team, seed data can help to make sure that all team members work with a consistent set of test data.
When we open seeds.rb for the first time, we see that it comes with pre-filled instructions:
These instructions are pretty easy to follow. We use the create
method to create instances of our model objects as collections of hashes, where the key is the name of our model attribute and the value is the attribute’s value. Another example comes from [My Recipe Box] (https://github.com/yvoloshin/MyRecipeBox), a recipe-sharing app I put together recently. In this app, a recipe has a :name, :ingredients, :instructions, and belongs to :user. I used the code below to create two seed recipes in seeds.rb:
Once the seed.rb is set up, all we need to do is run rake db:seed
and the seed data will be added to the database.
An important gotcha is that, when specifying the user (user_id: 1
), a user with this id must in fact exist in the database. If there is no such user, then the app won’t know what to do with this information and the data will be rendered incorrectly. Another useful point to know is, what would happen if we run rake db:seed
more than once? It turns out that the seed data is appended to the database every time we run the seed command. Seeding does not affect any database contents that were there previously. But, if we do want to erase the database and replace it with seed data, the command to run is rake db:reset
.
Let me know what you think of this article on twitter @YuryVoloshin or leave a comment below!