Advanced Active Record
Advanced Active Record
$20.00
Minimum
$20.00
Suggested
Advanced Active Record

Last updated on 2018-01-15

About the Book

You've probably been working on Ruby on Rails applications for quite a while. You've seen a lot of applications, a lot of upgrades, and a lot of refactorings.

So you already know the Active Record basics. You know how Active Record maps tables and columns into your model objects. You know how to use migrations to create new tables and alter the existing schema. You know the basics of associations, query expressions, scopes, and validations. You know a fair amount of Structured Query Language, or at least enough to execute straightforward queries in the console for your RDBMS. In other words, you're a competent developer when it comes to basic Active Record blocking and tackling.

That more or less where I am too, although I've been programming in Ruby for over a decade and programming Rails apps for slightly less time. Like you, I'm always on the lookout for ways to level up my programming skills. So I've greatly enjoyed some of the advanced Ruby books that have come out over the past few years. I'm talking about Pat Shaughnessy's "Ruby Under a Microscope", Jim Gay's "Clean Ruby", Paolo Perrotta's "Metaprogramming Ruby", and so forth.

But like you, I spend most of my time working on Rails applications. And there don't seem to be a lot of books covering more advanced Rails topics. If the code I've seen over the past decade is any guide, Active Record is one of the more challenging parts of Rails. I don't mean the basics; those are nicely covered in the Rails documentation and on many blog posts. But there's a shortage of technical deep dives into the more interesting Active Record situations. Thus, this book.

Really this book could be titled "Advanced Topics in ActiveRecord" or "Things That I Think You as a Fellow Experienced Active Record Developer Will Find Interesting". But both those would result in a much longer domain name. So let's just hold what we got.

Active Record is a complex part of a Rails application. From the perspective of Frederick Brooks' classic "No Silver Bullet" paper, this is essential complexity, not accidental complexity. At an architectural level, it is hitting another process using a language that's not Ruby. Think about the path that must be followed to execute a simple SELECT query on an existing table. First you add code to your application which encodes a static representation of looking up a row; that is, something like "Book.first". At runtime, that code is lexed, parsed, and translated into Yet Another Ruby Virtual Machine instructions for the Ruby runtime to execute. When the instructions are encountered and executed, Active Record generates a SQL query suitable for the database to which the application is connected. This SQL query is flattened into a stream of bytes and sent via the wire protocol to the RDBMS. The RDBMS generates or reuses a query execution plan based on table statistics and indexes and then executes that query against the underlying data. The results are serialized and returned to the Ruby process, which unmarshals that byte stream into objects. Those objects are further wrapped in Active Record-specific data structures to make them more accessible. What a journey! And that description is neglecting a variety of other layers that are out of scope for this book. You could build a career just by specializing in each step of the process. It's a rich field of study and one that rewards closer examination.

There's much more along these lines in the book. Enjoy!

Table of Contents

  • Introduction
    • Roads Not Traveled
    • Things Active Record Does and Doesn’t Do
    • The Domain Model
    • Suggestions Welcome
    • Acknowledgements
  • Dedication
  • Migrations
    • Using Generators
    • Coding Migrations
    • Working with Database Administrators
    • DDL vs DML
    • Onwards
  • Queries
    • Eager Loading and Joins
    • to_sql and explain
    • Table Aliases
    • Column Aliases
    • Subqueries
    • Terminators
    • The Attributes API
    • Eschew raw SQL
    • Structural compatibility
    • Onwards
  • Scopes
    • A Case for AREL
    • Single-record Behavior
    • Default Scopes and Unscoped
    • Scopes as Builders
    • Onwards
  • Associations
    • Better has_one
    • Association Extensions
    • Self joins and trees
    • Onwards
  • Code Traces
    • Attribute Names
    • Loading a record
    • Onwards
  • Odds and Ends
    • Strict Validations
    • UUIDs as Primary Keys
    • Know Your Shortcuts
    • Active Record and Its Gems
    • Existing Schemas
    • Schema Modeling
    • Improving Your Skills
    • Conclusion
  • Appendix A - The Domain Model
  • Appendix B - Major Changes
    • 5.1 - April 2017
    • 5.0 - June 2016
    • 4.2 - December 2014
    • 4.1 - April 2014
    • 4 - June 2013
  • Appendix C - Changelog
  • Notes

About the Author

Tom Copeland
Tom Copeland

Junior developer

Reader Testimonials

Andrew Browman
Andrew Browman

I've just solved several annoying Rails bugs thanks to @tcopeland's book Advanced Active Record. It's brilliant!

Tim Schmelmer
Tim Schmelmer

So much practical knowledge, great stuff!

The Leanpub 45-day 100% Happiness Guarantee

Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Write and Publish on Leanpub

Authors and publishers use Leanpub to publish amazing in-progress and completed ebooks, just like this one. You can use Leanpub to write, publish and sell your book as well! Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks. Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. It really is that easy.

Learn more about writing on Leanpub