75 Minutes of Instructional Video Included With the People's Choice Book Package! Learn More

Easy Active Record for Rails Developers

Master Rails Active Record and Have a Ton of Fun Doing It!

Easy Active Record for Rails Developers is the ultimate guide to mastering building database-driven Rails applications using the powerful Active Record ORM Framework. By following along with the development of a fun, web application for vintage arcade game aficionados, you’ll learn how to:

Table of Contents

Easy Active Record for Rails Developers is divided into eight chapters covering the fundamental topics most critical to today’s Rails developer.

Chapter 1. Introducing Models, Schemas and Migrations

In this opening chapter we’ll get acquainted with many of the fundamental Active Record features you’ll use throughout your project’s lifetime. You’ll learn how to create the models used to manage the application’s business logic and provide convenient interfaces to the underlying database. You’ll also learn how to use a great feature called *migrations* to create and evolve your database schema. The chapter concludes with a section explaining how to begin testing your models with RSpec and factory_girl.

Chapter 2. Loading, Validating and Manipulating Data

In this chapter you’ll learn how to easily load, or *seed*, your application with an initial data set, as well as create and manipulate data. Of course, because we’ll want to eliminate all possibilities of incomplete or invalid data from being persisted to the database, you’ll also learn how to enhance your application models using a variety of *validators*. Additionally, we’ll build on the introductory model testing material presented in the previous chapter. Notably, you’ll learn how to create tests for ensuring you’ve properly fortified your models with enough validators to ensure unwanted data can’t slip through the cracks.

Chapter 3. Querying the Database

In the last chapter you learned how to load seed data and create records, now it’s time to begin querying that data! In this chapter I’ll introduce you to Active Record’s array of extraordinarily powerful query features. By the conclusion of this chapter you’ll know how to efficiently query for all records, retrieve a specific record according to primary key, select only desired columns, order, group and limit results, retrieve random records, and paginate results. You’ll also learn how to integrate these queries with the application controller and view to create listing and detail pages. Always striving towards writing readable code, we’ll also enhance the models using a fantastic feature known as a *scope*.

Chapter 4. Introducing Associations

Building and navigating table relations is an standard part of the development process even when working on the most unambitious of projects, yet this task is often painful when working with many web frameworks. Not so with Rails. Thanks to a fantastic feature known as *Active Record Associations*, defining and traversing these associations is a fairly trivial matter. In this chapter I’ll show you how to define, manage, and interact with the `belongs_to`, `has_one`, `has_many`, `has_many :through`, and the `has_and_belongs_to_many` associations.

Chapter 5. Mastering Web Forms

Your application’s web forms will preferably interact with the models, meaning you’ll require a solid grasp on Rails’ form generation and processing capabilities in order to integrate with your models in the most efficient way possible. While creating simple forms is fairly straightforward, things can complicated fast when implementing more ambitious solutions such as forms involving multiple models. In this chapter I’ll go into extensive detail regarding how you can integrate forms into your Rails applications, covering both Rails’ native form building solutions as well as several approaches offered by popular gems.

Chapter 6. Debugging and Optimizing Your Application

While it’s fun to brag about all of your project’s gloriously cool features, they came at great expense of your time and brainpower. Much of the effort was likely grunt work, spent figuring out problems such as why a query was running particularly slow or the reason a complex association wasn’t working as expected. This important but decidedly unglamourous part of programming is something I try to minimize on every occasion by relying on a number of tools and techniques that can automate much of the analysis and debugging process. In this chapter I’ll introduce you to several of my favorite solutions for debugging and optimizing Rails applications.

Chapter 7. Integrating User Accounts with Devise

Offering users the opportunity to create an account opens up a whole new world of possibilities in terms of enhanced interactivity and the opportunity to create and view custom content. Yet there are quite a few moving parts associated with integrating even basic account features, including account registration, secure password storage, sign in, sign out and password recovery interfaces, and access restriction. Fortunately the fantastic Devise gem greatly reduces the amount of work otherwise required to implement account creation, authorization and management features, and in this chapter I’ll introduce you to many of the wonderful features this gem has to offer.

Chapter 8. Powerful Active Record Plugins

Ruby on Rails is such a powerful solution thanks in large part to its enormous ecosystem. You’re probably familiar with prominent gems such as Devise, RSpec, and FriendlyId, but there are also much more focused gems capable of extending specific parts of Rails, including templating (such as Redcarpet) and e-mail (see the popular Mail gem). So too can you extend Active Record in a variety of interesting and useful ways, and in this chapter I’ll introduce you to a few of my favorite gems, including Acts As Taggable On, Decent Exposure, and Geocoder.

Frequently Asked Questions

What is Active Record?
Active Record is a pattern that describes the creation, manipulation, and retrieval of business objects requiring persistent storage within a database. The Ruby on Rails framework includes a particularly practical and capable Active Record implementation that helps Rails developers to quickly and easily build powerful database-driven applications.

What is Easy Active Record for Rails Developers?Easy Active Record for Rails Developers is the most comprehensive book yet written on Rails’ Active Record implementation. Among other topics, the book covers model generation, migrations, validations, associations, joins, includes, forms integration, and query optimization. The book is now available!

About the Author

I’m Jason Gilmore, a web developer with more than 15 years of professional experience working with clients of all shapes and sizes. I’m the author of several bestselling books, more than 300 published articles on various technical topics, and am a co-founder of the CodeMash Conference. E-mail me or follow me on Twitter.

Packed with Information

About the Companion Project

Readers follow along with the creation of ArcadeNomad, a location-based Rails application tracking the locations of retro arcade games. Readers can optionally purchase all of the same source code used to run the
live ArcadeNomad website! Learn more.

Free Rails Tips, Tricks and More

Subscribe to the EasyActiveRecord.com newsletter and receive the first two chapters free!