0.3 Getting started with ActiveRecord

A quick sidetrack

In the last few lessons, we went through the basics of a request-response cycle in Rails and took a stab at HTML. But web applications are all about dynamic content - being able to receive, store, process and render data in meaningful ways. Relational databases are the primary mechanism through which most websites achieve this. In this lesson, I will take you through the basics of a database to help you bend it to your will using ActiveRecord.

It is all about good food

A database is simply a collection of tables. Each table is stored as rows and columns.

Dish Time to prepare Ingredients
Dresil 50 mins 2 cups rice, 6 tablespoon butter, 1/2 cup cashew, 1 cup raisin
Milk Tea 5 mins 1/4 cup Milk, 3/4 cup water, little sugar

The above is what I'll henceforth call a Recipe table.

We will be working with variations of the Recipe table in this introduction. Because food is yum and everything!

To begin, type the following into the console and run it.

class Recipe < ActiveRecord::Base
end

puts Recipe.all

Output Window

Excellent.

Let us go over the terminology a bit before diving into the deep end. In the first line, you defined Recipe as an ActiveRecord class. An ActiveRecord class is a magical beast. It gives you a beautiful set of methods to manage the data in the underlying table. all is one such method (you now know what it does, don't you?)

Over the course of this chapter, I will help you appreciate enough of ActiveRecord to be dangerous. You will be able to build software that can help you answer the following questions:

  • Give me a dish that I can prepare in 10 minutes
  • I don't like spicy food. What are all the Indian dishes that doesn't have chillies? (if they exist!)
  • I have just onions and eggs in my kitchen. What can I make?

(If you find these questions interesting, there is the lovely Cucumbertown that answers these and more!)

Querying

The all method is the simplest query you can send an ActiveRecord object. It returns you an array of all the rows in the table.

You would however often need to ask certain questions to the database and see only rows that match specific conditions. Like say:

Example Code:

Output Window

Isn't that intuitive?

Can you now fetch just the recipe to make Dresil? It is not that hard - use the where method as you saw above and just change the condition to use the dish column. Try?

Output Window

You now know the basics of querying a table using ActiveRecord. It isn't a trivial feat - you can easily build upon this to write advanced queries and answer complicated questions. Like this one: What are all the Indian dishes that doesn't have chillies?

Adding rows

There are simple things that are apparent on your first gaze. And there are other boring things that people claim takes a lifetime of study like the meaning of life and everything.

ActiveRecord luckily falls under the first category. Let us see - read the following code and then click Run:

Example Code:

Output Window

Now use the same approach to create a new dish Potato Chips with ingredients Peeled Potato, Water, Butter and time to prepare as 20 mins:

Output Window

Modifying contents of existing rows

They say that change is a constant or some similar axiom of the profound nature. Rows in database tables aren't free from change either. How do we go about changing existing data in ActiveRecord?

As the first step, you should retrieve the row you wish to change - that much should be pretty obvious. But how?

Example Code:

Output Window

That gives you the first row from the table. (ActiveRecord also gives you a last method to fetch the last record).

Let us now see how we can fetch a record from the table and modify its attribute:

Example Code:

Output Window

Now try changing the time to prepare Potato Chips to 30 mins:

Output Window

You would have observed that you need to use the `first` method extract extract the first element from the array. This is because where always returns an array of rows.

You now know the basics of manipulating a database table using ActiveRecord. In the upcoming lessons we'll go back to Rails and use our knowledge of ActiveRecord to build a Recipe website.

Do you have any questions so far? Let me know! Suggestions and feedback welcome. Click here to message me.

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2014 by Jasim A Basheer