Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Exclude Fields from Eager Loaded Models

#3662

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.

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

State: stale
Milestone: none
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to stale
    • Importance changed from to Low

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 8th, 2010 @ 12:36 PM

  2. AlexParamonov
  3. Rohit Arondekar
    Rohit Arondekar
    • State changed from stale to open
    • Assigned user set to Santiago Pastorino

    Santiago: Could you please take a look at this ticket? I'm assigning to you since you've worked on AR and AMo a lot. :)

    February 3rd, 2011 @ 01:12 AM

  4. rails
    rails

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    May 4th, 2011 @ 01:00 AM

  5. rails
    rails
    • State changed from open to stale

    May 4th, 2011 @ 01:00 AM