Understanding ActiveRecord: A Gentle Introduction to the Heart of Rails (Part 2)
Pages: 1, 2, 3, 4

Back to the Browser

Here's an updated view for entries to show you the new features visually:

    <h2> List of all entries </h2>                                                   
      <% @entries.each do |e| %>                                                     
        <li><%= e.short_description %> ( <%= e.url %> )<br>                          
            Tags: <em><%= e.tag_names.join(" , " ) %></em><br>                       
            Posted By: <%= e.user.display_name %><br>                                
      <% end %>                                                                      

If all goes well, when you browse to http://localhost:3000/entries you should see this:


It'd be nice to see a view of the tags, too, since we went through the added trouble to create them. Generate the controller like this:

$ script/generate controller tags

My controller and view for tags are shown below:

class TagsController < ApplicationController                                         
  def index                                                                          
    tag_names = Tag.find(:all).map(&:name)                                           
    @urls_for_tags = {}                                                              
    tag_names.each do |name|  
      @urls_for_tags[name] = Tag.entries_by_name(name).map { |r| r.url }             

<h2> URLs for All Tags </h2>
   <% @urls_for_tags.each do |tag,url_list| %>
   <h3><%= tag %></h3>
     <% url_list.each do |url| %>
       <li><%= url %></li>
     <% end %>
   <% end %>

Assuming all goes well, http://localhost:3000/tags should look like this:


Not exactly "beautiful," but definitely functional. With a little form building love and some linking, you pretty much have an app.

The Light at the End of the Tunnel

ActiveRecord is a big system with a whole lot of features. The good news is, if you've been able to follow the code in this article, you've just covered a huge chunk of the most common database tasks you'll need to do in your Rails applications. The tricky stuff comes easy once you get past the first hurdle of actually getting something running.

Because I couldn't delve too deep into any given topic, there are definitely some holes you'll want to fill in if you want to master ActiveRecord as a whole. In terms of relationships, I left out has_one because it should be easy to understand now that you know how to do the more complicated associations shown. I've also left out the important topic of polymorphic associations, which can be very powerful for certain types of modeling. You'll also want to dig a whole lot deeper than I did in this article in terms of testing your applications, which is something that you'll always be thankful for in the long run.

Beyond that, there is a lot more interesting stuff that could be said in advanced ActiveRecord discussions. But most of the stuff you need to know, you'll be able to find when you need it. For now, you probably have the skills necessary to populate the world with yet another socially-networked, uber-cool, ultra-sexy whathaveyou.

The cool thing about ActiveRecord is that it is useful both in general Ruby applications, as well as in the quirky-yet-lovable web framework Camping. So if Rails isn't the only thing on your mind, having a solid base in AR may still be useful to you.

The full source for the code from this tutorial is available under the License of Ruby, so if you'd like to play with it, feel free.

Gregory Brown is a New Haven, CT based Rubyist who spends most of his time on free software projects in Ruby. He is the original author of Ruby Reports.

Return to Ruby.