Lesson 2
Anatomy of a Web Application
Learning Rails, episode 2: Anatomy of a Web Application
Welcome to the second episode of Learning Rails. I’m Michael Slater.
And I’m Christopher Haupt.
In our first episode, we explained what Ruby on Rails is, and why it is worth investing your time to learn. In this episode, we start to look at how a Ruby on Rails application works.
Before we get into Rails, we need to explore Ruby and object oriented programming briefly. There’s a few key concepts that we use in the following section, and it’s important that you understand what we mean by them so you can follow the bigger story of how Ruby on Rails works.
Some Key Programming Concepts
In this section, let’s cover a very high-level primer of some the basic programming terminology we’ll be using: variables, objects, classes, and methods. If you are already familiar with these concepts, feel free to skip ahead.
We will often need to store information in our programs. The container that holds this information is called a variable. A variable in Ruby is very flexible and doesn’t require you to either declare or define it before you use it or even specify what kind of data it will hold. Variables can represent simple data, such as numbers or strings of characters, or they can hold more complex information, such as lists of data (called Arrays in Ruby) or data composed of a collection of simpler types.
As you learn about Ruby and Rails, you may hear the term “scope” in reference to variables or the objects they contain. We’ll skip the nuances now, but you should understand that variables have a lifetime within your program and may have limitations as to where they may be used.
Variables have names you give them and both Ruby and Rails have a variety of naming requirements and conventions that you will grow familiar with overtime.
Ruby is known as an Object Oriented language, and everything in a Ruby program is some kind of Object. OK, so what is an Object? An object is something that packages together simple or complex pieces of data along with any behaviors for working with that information. A number object, for instance, holds the numerical value data and includes behaviors such as addition, subtraction, and equality testing.
Besides the built in Objects of Ruby, you can define your own objects that represent pretty much anything you can imagine. To do this, you create a Class. Classes are special objects that act as blueprints for Ruby to use when you request that it construct your objects in memory. Within a class, you specify the name of the object type you are creating, what data it will hold, and any messages it can receive to trigger its internal programs. These little programs in an object are called “methods”, or sometimes “functions”; and as we’ll see shortly, in the context of a Rails controller they are called actions.
Ruby classes are organized in something like a family tree: you can define classes that inherit attributes or behavior from other classes, in a simple parent-child relationship. This enables you to organize your data in a hierarchy that starts with more general features at the top and more specific ones descended below. An example is a Person Class and an Employee class. The Employee class is a sub-class of (i.e., inherits from) the Person class, and it inherits attributes such as a Name that all people have. The Employee sub-class might add the idea of Employee ID Number, which Employees might have, but not all People.
While you might not use inheritance right away for your own classes, it is used throughout Ruby and heavily in Rails. In many cases, you enjoy its benefits without needing to know how it works. Your model objects, for example, start life with a rich set of methods for finding and accessing information, which they inherit from an Active Record class.
OK, we’ve defined, in a simple way, the terms Variable, Object, Class, and Method, so let’s jump back to how Rails works to process a request from a user.
From URL to Server Response
With this bit of programming terminology behind us, let’s review just what happens when a user views a web page. We tend to this of this as a simple action, but actually there’s a lot involved.
First, the user enters a URL in their browser, or they could click on a link in another web page. The browser sends a request to the server specified in the URL (that’s everything before the first slash), asking for the page specified in the URL. The server returns the HTML page.
For example, suppose the URL is www.BuildingWebApps.com/podcast.html. podcast.html is the name of a file on the server that contains the HTML code for the page. The server doesn’t do much processing; it simply returns the HTML to the browser. The browser reads the HTML, and when it sees references to other files, such as CSS stylesheets, images, and JavaScript files, it sends requests for those files too.
With a typical PHP site, the process is only slightly different. The URL would end with podcast.php, which is as before the name of a file on the server. In this case, however, the file contains a mix of PHP programming language instructions and HTML code.
The server now does more work; instead of simply sending this file to the browser, it executes the parts that are PHP code, sending to the browser the results of running this code, along with any regular HTML that was in the file. The PHP code might, for example, read from a database to get a list of podcast episodes, and then generate HTML code that would display that list.
In both the HTML and PHP examples, there is a one-to-one correspondence between URLs requested by the browser and files stored on the server. The server just requests the files that have been asked for, and if they contain any program code, executes that code before sending the results to the browser.
In a Ruby on Rails application, the flow is more involved. There typically would not be a file-type extension on the URL; it is simply www.buildingwebapps.com/podcast. The name podcast is not the name of a file at all, but the name of the request that the browser wants to make of the Ruby on Rails application.
The server therefore has a more complex job to do. Instead of simply grabbing the file whose name corresponds to the request, the request is passed to the Rails routing code: the part of the Rails framework that processes incoming requests.
There’s a file called routes.rb, which is part of every Rails application, that the Rails code executes to process each request. In this file, you can customize the routing to a great degree, or you can specify some simple, default routes that apply to most requests. But one way or another, when the routing code is done executing, Rails has chosen a controller to process the request, and an action to invoke in that controller.
For example, if our podcast controller has an action called show, the URL www.buildingwebapps.com/podcasts/show would trigger that action. Typically there would be a database ID at the end of the URL (such as podcasts/show/17), this value is passed to the show action.
Controllers
Once a request has been routed to a particular action in a particular controller, it’s up to that controller to move things along.
A controller is a Ruby program (almost everything in Rails is a Ruby program) that processes one type of related incoming request. For example, our podcast controller deals with everything that has to do with podcasts. The controller is divided internally into a number of methods that perform very specific tasks. Most controller methods are just a few lines of code. Best practice typically puts anything complicated in the model objects and presentation is handled in view objects.
For example, the podcast controller has one method to display the podcast main page, with a listing of all the episodes. There’s another method to display the show notes for a particular episode, and another to display the transcript for an episode. There may be additional methods, which require an administrative log-in to be able to access, for uploading a new podcast or entering the text for a podcast’s title, description, notes, and transcript.
Controller Actions
Controller methods are usually called actions. An action is a method that can be invoked from a particular URL. Remember, everything starts when someone clicks on a link or enters a URL in their browser, and the URL is sent to the server. In response, Rails finds the corresponding action to execute.
Controller have default actions to execute when an incoming URL omits a specific action. If an action called index is supplied, it is used. For instance, when a user browses to www.buildingwebapps.com/podcasts, the default action “index” in the podcast controller executes.
The index action itself simply fetches the list of episodes from the database. That’s all it needs to do; as we’ll see shortly, the view takes care of the rest.
If you’ve built a PHP application, you’re probably used code that directly sends a query to the database, and you had to write that query in SQL and fetch results a row at a time. In a Ruby on Rails application, model objects handle all communication with the database. The model is the abstract representation of your application’s data, independent of any particular way of viewing it.
Every model object inherits a rich set of capabilities from Active Record, a library that is a key part of Rails, so your models come to life with a rich set of capabilities without your writing a single line of code. And when you do write code in your models, you can work at an abstract level, while Active Record takes care of all the details of working with the database.
We’ll skip over the details of the language syntax for now, but in essence, the controller would include a statement like “podcasts equal podcast find all”. In this statement, the first word, podcasts, is a new variable that we’ve just created to hold the list of podcast objects. When we write “podcast find all”, we’re acting upon the podcast model (invoking a find method on the Podcast class, to be precise), and asking it to find all podcast objects. This find method is one example of the magic that model objects are born with, thanks to Active Record. You can even write statements like podcast find by episode 2, to find the podcast object whose episode number is 2.
You don’t have to think about SQL, or how to access the database, because all of that is handled by the Rails framework code, with a little help from the podcast model file. You tell Active Record what you want to do to the model objects, and Active Record creates SQL statements to do it.
The controller’s job is now done. Controller actions are often just a few lines of code. They make requests from the model, as needed to fulfill the request that just came in from the browser, and put the results into variables that the view uses to get data for the page.
Rails Views
What’s next? The view is invoked. The view is another file, separate from the controller and model files, that is specific to a single action in a single controller. In this case, Rails invokes for us the index view associated with the podcast controller. The view has access to the podcasts variable that the controller set, so it doesn’t need to talk to the database, or even to the model; it just works with data that has already been selected for it and stored in this variable.
The view includes all the presentational aspects of the page — everything about how the page looks. The parts of the page that are common across all pages, such as the header and footer, are provided by a separate file, the layout, which Rails automatically invokes for us before it begins processing each view. So a view only needs to concern itself with things that are specific to a particular type of page.
A view typically includes a mix of HTML and Ruby code, much like a PHP file includes a mix of HTML and PHP. But a typical PHP program, if built without a model-view-controller framework, puts into a single file not only all the view code but also aspects of the model and controller functions. In the Rails approach, you have many more files, each of which is responsible for a very specific part of the task of collecting information, formatting it, and rendering a web page.
A typical Rails view includes the HTML-coded text that fills most of the page, and a small amount of Ruby code. In our podcast listing example, the Ruby code uses the podcast variable the controller provided to generate the list of podcasts.
The view code iterates through the array of podcast objects stored in the podcasts variable. It reads each podcast object in turn, using the methods the podcast object provides to access each of its properties. For example, the podcast object provides a title method, which returns the name of the podcast; a description method, which returns a block of descriptive text; and other methods to provide links to the show notes, the transcript, and the audio file.
More about Models
Rails uses the ActiveRecord library to implement a technique called object-relational mapping. This allows you to think in terms of objects in your Rails program and let the underlying library deal with most of the complexity of storing and retrieving information from a Database. This is particularly nice as you don’t have to become an SQL expert and can stick to Ruby coding in most cases.
When you are building your application, you can think in terms of the abstract concepts you want to model, like a Podcast object. Rails provides tools for easily translating your ideas into the storage mechanism of your database. These tools not only set-up the database structure for you, but also generate the Classes and their default behaviors that you’ll use when running your program.
There are two basic steps to you will take time and time again. First, you will spell out the data you need for each of your Classes using a simple database neutral syntax in Ruby. These definitions sit inside of one or more files called migration files. Migrations are special programs that Rails uses to setup and maintain the database structure on your server. Second, you create model files, which are classes that derive behavior from ActiveRecord.
Your model classes gain a lot of functionality due to their inheritance from Active Record classes. When Rails creates a model object on your application server from one of these classes, it knows to go out to the database server, scan the database’s structure, and automatically build the object that corresponds to the database table holding that information.
The model layer in Rails deals with all of the communication issues between the application and database servers, so you don’t really have to worry about it, it just works. If, over time, you need more sophisticated data manipulation beyond Creating, Updating, Finding, or Deleting objects, you can dip in to lower level capabilities or write your own SQL.
Model objects generally come in to existence while a controller action is running. This typically means that model objects come and go every time you visit a page. In our Podcast example, once the podcast index listing is rendered by the view, and the controller action finishes, the podcast objects disappear, and the memory they used is freed. The information is still in the database, of course.
Wrap Up
We’ve now see how, when an incoming request is processed by a Rails application, the server chooses a controller to invoke, and an action to execute within that controller. The action uses model objects to request information from the database, which the controller stores in a variable for the view to use to render the HTML page.
We’ve completed our brief tour of how Ruby on Rails uses the Model-View-Controller pattern and we’ve taken a quick peek into how controllers, views, and the model system operate. Next episode we’ll dive deeper into views and see how the template system works.

Reader Comments
32 comments
From: nitesh , 08/13/11 11:41 AM
EXCELLENT .... LUV IT
Perfect!
From: Richard, 04/24/11 07:15 PM
I'm a rails newbie. This makes rails So. Much. Clearer. Rails for Zombies misses some key explanations about how Rails holds together, so perfect supplement.
model view controller
From: Dan B, 02/28/11 10:47 PM
excellent description of model view controller theory
how to install ror in windows
From: joel joseph, 02/08/11 04:52 AM
i tried it many times.. but some errors are coming while installation. when i asked a ror delevoper . he said mostly people do ror in linux or ubuntu . Is that so?..
MVC Anatomy
From: K, 01/25/11 04:40 PM
Thanks!
lesson 2
From: Patrick Hop, 07/16/10 11:19 PM
thank you!
Querry
From: Sunny, 02/21/10 08:18 AM
In rails model deal with DB. all DB queries should have in model? is this right or we can write some DB query in controller itself?
Querry
From: Sunny, 02/21/10 08:18 AM
In rails model deal with DB. all DB queries should have in model? is this right or we can write some DB query in controller itself?
Querry
From: Sunny, 02/21/10 08:18 AM
In rails model deal with DB. all DB queries should have in model? is this right or we can write some DB query in controller itself?
Best way to start rails in mac
From: Runy, 01/25/10 10:20 PM
Hi what is the best way to develop rails applications on the mac? I'm using instantrails on windows which is great because it has mysql build in. I needed mysql on the mac but didn't get it working. Radrails on mac from aptana is horrible because nobody seems to be able to get it working with mysql.
Thanks!!!!
From: mr49online, 09/15/09 09:54 PM
Thanks a lot, for giving such a to the point lessons for learning newbie in rails, like me ... Keep it up....
learning rails-web anatomy
From: Jitesh Dedhiya, 07/17/09 07:07 AM
Hey! thanks for these series.Helping me a lot to know about ROR.The Simplicity of lessons is helping me to understand very fast.The way the lessons are introduced step by step are great.Great work guys!!!
learning rails-web anatomy
From: Jitesh Dedhiya, 07/17/09 07:07 AM
Hey! thanks for these series.Helping me a lot to know about ROR.The Simplicity of lessons is helping me to understand very fast.The way the lessons are introduced step by step are great.Great work guys!!!
learningrails podcasts
From: eric, 02/17/09 01:46 PM
Thanks very much for this series. I've tried to dive into rails twice before and become frustrated by a certain point. Conceptually, rails is a steep learning curve, there seem to be a lot of things to comprehend at first, it's fat at the bottom. Having listened to your first three podcasts several times over I'm beginning to see that, although there are many pieces each piece is small and managable. I'm ready to start try again.
Ruby Software
From: Michael Slater, 01/24/09 05:36 PM
See http://www.buildingwebapps.com/articles/6491-setting-up-rails-on-windows-vista
Ruby Software Download
From: Herb Walfoort, 01/24/09 04:55 PM
Hi What is the URL to Download the Ruby program for use on Windows Vista? Thanks in advance Herb Walfoort
Thank you for all lessons
From: Zeck, 10/02/08 08:16 PM
First of all thank you Mr Michael and Christopher. I think this is a best free online screencast. Looking forward to the your next one
Lesson#2
From: cherrian chin harada, 09/21/08 06:42 AM
Michael & Christopher, my sincere thanks to you both... Love your lessons. Looking forward to the your next one :-) lesson
Lesson 2
From: Charlz, 08/07/08 10:13 PM
Hope next time we can see some more examples from the audio.
Credit to...
From: Thura, 07/20/08 02:49 PM
Thanks…good concepts/points on ruby on rails.
NO much images
From: Julio, 07/04/08 12:17 PM
I haven’t a great Internet line, so don’t put much images, or maintain one page without them, please…
FROM URL TO SERVER RESPONSE
From: Aron Grinshtein, 06/23/08 12:55 AM
I didnt understand the difference between how regular HTML and PHP sites process going to a website and how RUBY processes visiting a website.
Please elaborate, a diagram may help.
Thanks!
Very interesting course!
From: Jesus Armando Garcia Quiñones, 06/18/08 09:36 AM
Hi to all! i’m from Mexico and want to be thankful to them for this opportunity to learn RoR. i was reading many text but, in this case i consider is very clear and easy to follow. Thank’s and good work!
PD. Excuse me if my English not is very good.
Nice course!
From: Murillo Parreira, 06/14/08 03:45 AM
Hi folks, i’m from Brazil and around here, RoR isn’t very popular… Despite this fact, i’ve been studying on my own and learning to really like Ruby and Rails by the simplicity and ‘beauty’ of the code…
Just want to say: ‘Keep up the good work!’ The iniciative is really inspiring!
=D
Great! Add images!
From: Rich Webster, 05/14/08 12:05 PM
This is the best free RoR thing I’ve found. By following the text while listening, it somewhat satisfies my learning style, but diagrams of the relationships, and ultimately (when you get down to code) having video of the code, as it is entered, would be great. I don’t think audio alone will cut it for me, in the long run. I would be OK with inline examples on the transcript page, but that would not be ideal.
I just finished an online class thru a community college, and this is at least as helpful, so far. The instructor in that class provided videos, but he was less organized at communicating the concepts. I also like the two-voice technique. Keep that up.
Learning Rails
From: Phil Leeson, 04/26/08 08:50 AM
Excellent – very clear and concise and also very practical. These tutorials are just what I need. Thanks!
Very Informative
From: Naushad Pasha, 04/21/08 07:10 PM
Its clear and understandable, Well only suggestion is we should have the video lessons also so that any person who is very new to rails will understand better.
Thanks Naushad Pasha
good~
From: iceskysl, 04/18/08 11:39 AM
very clearly and useful.
thanks!
From: zain alabdin tawfiq, 04/12/08 06:55 AM
I don’t know how to thank you, I’m searching for ROR tutorials and this seems like a good one!
Text is available
From: Michael Slater, 04/09/08 01:46 PM
If you’re looking for text versions of the audio lessons, just click on the Transcript link on the left side of the lesson page.
Screencasts start soon
From: Christopher Haupt, 04/07/08 12:40 PM
Stay tuned, as we’ll be posting screencasts starting with Episode 9.
Text lessons
From: Muhammad, 03/30/08 12:09 AM
Hi manager, can you send me an text lessons because I don’t understand it very really :-) thank you Muhammad