Advanced Active Record
$20.00
Minimum price
$20.00
Suggested price

Advanced Active Record

A Collection of Practical Deep Dives into Active Record Topics

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!

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!

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

The Leanpub 60 Day 100% Happiness Guarantee

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

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

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. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub