Getting Started
The Ruby for Rails Tutorial went over Ruby and built a simple Rails-style app. The Ruby on Rails Overview introduced the different parts of Rails, and now it's time to code your own Rails application. This tutorial will just focus on the data side of a web application, so you can store and retrieve data from a command line, but without a web interface. Future tutorials will show how to turn this start into a complete web application.
Environment
Beginners often run into issues when setting up Rails on their computer, so the easiest way to get started is to use a cloud IDE, an online development environment. Cloud9 is a powerful cloud IDE with a good free tier, so sign up for an account.
Optional video
Here's some brief instructions to get started:
- Sign up for an account on c9.io
- Click CREATE NEW WORKSPACE > Clone from URL
- Paste in the following URL https://github.com/Learneroo/automated-store.git, click on "Ruby" and click "Create".
- Once the environment is set up, click on "Start editing"
This will open the Cloud9 editor so you can start developing your project! There are a lot of folders and files that Rails provides, but don't worry about them now.
Terminal
Optional video
The terminal (on the bottom of cloud9) is where you enter Rails commands. To complete the installation of this Rails app, enter the following in the terminal:
bundle install
Rails will then install the ruby libraries that your application uses.
Web Applications and Databases
We're going to build an application for storing and retrieving product data. Each product should have a name, a description and a price. The application should let an administrator Create product records, Retrieve/Read them, Update them, and Delete them. These 4 actions are called CRUD, and they're the main thing web applications do. Standard web applications save information (such as products or status updates) in a database for people to read, and perhaps update or delete.
Databases
This is a table of data, which could appear in a spreadsheet:
name | description | price |
---|---|---|
"Cow" | "Moos, eats grass" | 10 |
"Potato" | "Starchy and nutritious" | 2 |
"Parakeet" | "Chirps, eats seeds" | 2 |
"Phonograph" | "Records sound" | 4 |
A database is similar to a spreadsheet, but it used to store and retrieve large amounts of website data that can be modified and queried in many different ways. Each row (or "record") in a database represents a single item, such as a product or tweet. Later we'll learn more about a database's capabilities for storing and retrieving data, and connecting data together.
Creating a Product Model
In order to create and retrieve product records, we need to create a Product model for storing product information. This product model will let you interact with products as objects in Ruby code, and also save the information permanently to a database. To create the model, enter the following command in your terminal:
rails generate model Product name:string description:text price:integer
The terminal should show what files were created, which should look similar to this:
invoke active_record create db/migrate/20150324215138_create_products.rb create app/models/product.rb ...
The file app/models/product.rb
is the Product model, which you can open by navigating to app/models and clicking on product.rb
:
class Product < ActiveRecord::Base
end
There isn't much code here, yet this class already has many capabilities. How can that be? Since it inherits from ActiveRecord::Base
, a Rails library class that provides powerful features for dealing with databases from within Ruby.
Creating the Database Table
A Product class saves its data in a database table, but we didn't create any database table yet. Luckily Rails makes this easy for us by creating a migration file to create a database table that will automatically be connected to the product model. This is the first file that rails created above. Open up this new file (located in db/migrate
) to see what it contains:
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.text :description
t.integer :price
t.timestamps null: false
end
end
end
You don't need to worry exactly how this code works, but you can likely guess what it does. When executed, it will create a database table called products
with columns for name, description and price. Let's go ahead and execute it by entering the following code in the terminal:
bundle exec rake db:migrate
You should see output similar to this:
== 20150324215138 CreateProducts: migrating =================================== -- create_table(:products) -> 0.0026s == 20150324215138 CreateProducts: migrated (0.0027s) ==========================
That's it. You just entered two lines of code and you now have a working Product model connected to an actual database table! Next, we'll CRUD some products!