Sample Data

Optional Node
Premium Content - Free Preview

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:


End of Free Content Preview. Please Sign in or Sign up to buy premium content.

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