This project is archived and is in readonly mode.

#3662 ✓ stale
Josh Symonds

Exclude Fields from Eager Loaded Models

Reported by Josh Symonds | January 5th, 2010 @ 08:00 PM

I have an organization that has many mailboxes, each including some amount of email. I wanted to eagerly load this association like so:

@organization.mailboxes.find(:all, :include => :emails)

Mailboxes have lots of emails, which is fine. But those emails have some very big columns (like body), which I don't necessarily always want. Loading them into Rails slows down this query quite a lot, and unfortunately I have no way to specify that I don't want to load the body in this include. So I made a patch to allow this kind of syntax:

@organization.mailboxes.find(:all, :include => {:emails => {:except => :body}})

A select statement will automatically be generated to exclude the offending column. It also accepts an array of columns:

@organization.mailboxes.find(:all, :include => {:emails => {:except => [:body, :huge_column]}})

Both body and huge_column will be excluded from the select statement.

The patch also responds correctly to through associations and nesting:

@organization.mailboxes.find(:all, :include => {:emails => {:comments => {:except => :body}}})

I've included tests with the patch, and I'm eager for feedback if people think this would be a good addition to ActiveRecord.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Tickets have moved to Github

The new ticket tracker is available at https://github.com/rails/rails/issues

Shared Ticket Bins

Attachments

Pages