How to seed with Faker gem (Rails)
Faker library helps populate large database with real-looking test data in just seconds!
- Install Faker gem
gem install faker
2. At the beginning of your “seeds.rb” file, put
3. Now you can start creating data with faker gem! Example below shows how to create users.
4. You can do so much more with available categories listed here: https://github.com/faker-ruby/faker. Below are samples of categories.
5. Under each category, there are many sub-category to choose from based on your need.
Helpful Notes: below tips are not necessary while using faker gem but
- Code below is useful to add prior to create your seeds. So that if you need to re-seed your file, it will erase data from previous seeding.
2. The terminal will only tell you if there are issues with seeding, not if it’s completed successfully. Print a string at the end of your seeding code. So that you know it’s done!
3. If you want unique data, add the word “unique” in between the category and sub-category. For example, if I want to generate unique emails throughout my database; I would rewrite my code from step 3 to generate email as below: