Paranoia

February 24, 2007 Alex Chaffee

Hoping to improve performance, we changed a query to use the :include condition like this:

    Project.find(id, :include => :stories)

and we noticed two things:

  1. ActiveRecord decided to turn that into a LEFT OUTER JOIN. Egads! This drastically slowed things down (although we didn’t notice until several days later, when we ran a real load test with production data).
  2. acts_as_paranoid did not manage to stick it’s little “and deleted_at = nil” phrase into the query. This meant that “deleted” stories showed up when they weren’t supposed to.

So that’s two gotchas for the price of one.

In email, Nick pointed out that the joining behavior is documented and appropriate (”otherwise if there is a nil association (eg a project without any stories) you wouldn’t get a project back even though it exists!”) and Miho rejoined that AR can be dangerous because it changes what looks like beautiful, elegant Ruby into nasty, ugly, hard-to-understand SQL under the hood.

About the Author

Biography

More Content by Alex Chaffee
Previous
Subversion gotcha – deleted folders not shown in diff by default
Subversion gotcha – deleted folders not shown in diff by default

Say you have two tags you want to diff, and one has a deleted directory. If you do an 'svn diff', you won'...

Next
Do not fear the code — The code should fear YOU!
Do not fear the code — The code should fear YOU!

Never be afraid to refactor. It may seem scary right now, but it will only become more daunting as the cod...