Created
May 6, 2013 16:38
-
-
Save dbonates/5526285 to your computer and use it in GitHub Desktop.
#126 Populating a Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # bash | |
| gem install populator | |
| gem install faker | |
| rake db:populate | |
| # lib/tasks/populate.rake | |
| namespace :db do | |
| desc "Erase and fill database" | |
| task :populate => :environment do | |
| require 'populator' | |
| require 'faker' | |
| [Category, Product, Person].each(&:delete_all) | |
| Category.populate 20 do |category| | |
| category.name = Populator.words(1..3).titleize | |
| Product.populate 10..100 do |product| | |
| product.category_id = category.id | |
| product.name = Populator.words(1..5).titleize | |
| product.description = Populator.sentences(2..10) | |
| product.price = [4.99, 19.95, 100] | |
| product.created_at = 2.years.ago..Time.now | |
| end | |
| end | |
| Person.populate 100 do |person| | |
| person.name = Faker::Name.name | |
| person.company = Faker::Company.name | |
| person.email = Faker::Internet.email | |
| person.phone = Faker::PhoneNumber.phone_number | |
| person.street = Faker::Address.street_address | |
| person.city = Faker::Address.city | |
| person.state = Faker::Address.us_state_abbr | |
| person.zip = Faker::Address.zip_code | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment