Sample Data

Optional Node
Collapse Content

Sometimes you'll want to setup simple setup data for your app so it works and looks OK. For example, your store home page expects at least one product to exist, and otherwise the page will be broken. Your app may be installed in multiple environments so it would be good if there was a way to create sample data automatically.

Luckily there is, and you can read about it in this section. If you're not interested, you can skip this page and create your sample data manually.

seeds.rb

Open up the file db/seeds.rb. You'll find some comments about how to use the file. You can use the same commands you used in the terminal to create items in this file.

For example, to create a product, you enter the following code:

prod1 = Product.create(name: "Cow", description: "Moos, eats grass.", price: 10, category_id: 1}

You can also create multiple items at once. Let's create multiple categories:

categories = Category.create([{name: 'Animals'}, {name: 'Food'}, {name: 'Electrical Devices'}])

We may as well create a bunch of products to set things up:

products = Product.create([
    {name: "Parakeet", description: "Chirps, eats seeds.", price: 3, category_id: 1},
    {name: "Potato", description: "Starchy and nutritious.", price: 1, category_id: 2},
    {name: "Biscuits", description: "Carby and delicious.", price: 1, category_id: 2},
    {name: "Phonograph", description: "Sound-recording machine.", price: 5, category_id: 3},
                        ])

You can use any Ruby code you want here, so let's go ahead and use a loop to add a quantity to all the products:

products.each do |product|
    product.update_attribute(:quantity, 5)
end

Setting up the Data

Now that we created simple code to create products, let's go ahead and run it.

Enter the following command in your console to run the seed file:

rake db:seed

This will run the code in seeds.rb but still keep whatever else you created before. To reset your database and run the seed data, enter the following command:

rake db:reset

This is useful for getting rid of random data in your development environment and starting over from scratch.

The Rails Source

optional

When coding, you'll often reference your tutorial, the Rails Guides, or the Rails Documentation (which can also be viewed here). But sometimes they don't have the information you need about Rails. In these rare cases, you can always lookup the actual Rails source code to see the truth from the source. Try looking up the Rails source in the challenge below.

Challenge

Look up the different rake tasks in the databases.rake source file on Github. Lookup previously discusses rake tasks, i.e. seed, reset and migrate to see the actual code used by Rails.

The reset task mentioned above really consists of multiple other rake tasks. Can you see what they are in the source code? Copy the array of commands that are shown for task :reset

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Contact Us
Sign in or email us at [email protected]