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.

collection_singular_ids breaks when used with :include

#2896

Given a has_many association defined with an :include option, you can't use the #collection_singular_ids method because it fails trying to eager load the 2nd level association when it's unneeded.

The attached patch includes a test case where you can see the need for it.

I'm not exactly happy with the patch because it involves adding a new :ignore_include valid option to ActiveRecord::Base.find, but I think I've read people requesting that kind of option too so maybe this is something you may want too.

Please review the patch and suggest any improvements or raise concerns.

Reported by Diego Algorta · July 9th, 2009 @ 09:30 PM

State: resolved
Milestone: 3.x
Assigned to: Pratik Pratik
Importance: Low

Activity

  1. Michael Koziarski
    Michael Koziarski
    • Tag changed from :include, activerecord, eager_loading, patch to :include, activerecord, bugmash, eager_loading, patch

    August 3rd, 2009 @ 06:11 AM

  2. dira
    dira

    verified

    Updated just the tests from the patch to apply cleanly.

    I think there should be a cleaner implementation - so I did not include the original one in the patch.

    August 9th, 2009 @ 12:51 PM

  3. Jason Roelofs
    Jason Roelofs

    I just ran into this bug. Asking for collection_singular_ids shouldn't try to load any include-ed associations.

    November 19th, 2009 @ 07:05 PM

  4. Rizwan Reza
    Rizwan Reza
    • Tag changed from :include, activerecord, bugmash, eager_loading, patch to :include, activerecord, eager_loading, patch

    February 12th, 2010 @ 12:46 PM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Dan Pickett
    Dan Pickett
    • Tag changed from :include, activerecord, eager_loading, patch to :include, activerecord, bugmash, eager_loading, patch

    Can a bugmasher verify this errant behavior on master and supply an updated path?

    May 9th, 2010 @ 07:18 PM

  7. Diego Algorta
    Diego Algorta
    • Assigned user set to Jeremy Kemper

    I'm attaching two separate patches in case the core team doesn't like my fix, or someone shows up with a better one (which is quite possible).

    add_failing_singular_ids_with_include_test.diff is an updated version of the failing test needed to prove the bug.

    fix_failing_singular_ids_with_include.diff is the patch to fix activerecord so that the previously added test, passes. It's VERY different from my original patch when creating this ticket as rails itself has changed a lot since then. I still think someone with better knowledge of activerecord's internals could do it better, but this one seems to be good enough (and I think less messy than the previous one).

    Both apply cleanly on top of ce5827ea4791e8b8143919ecceb0231e36e8932e (master from may 9)

    May 10th, 2010 @ 09:27 AM

  8. Diego Algorta
    Diego Algorta

    I forgot to include one file in the test patch, So I'm attaching a new commit with the missing file.

    May 11th, 2010 @ 12:36 AM

  9. Diego Algorta
    Diego Algorta
    • No changes were found…

    May 11th, 2010 @ 12:47 AM

  10. Federico Brubacher
    Federico Brubacher

    The last patch looks much better than the first time, i ran the tests on latest version of master and so far so good. It seems a issue worth fixing.
    +1

    May 11th, 2010 @ 01:26 AM

  11. Diego Algorta
    Diego Algorta

    As there where too many patch files already, I deleted the ones which were mine and I'm submitting them merged in just 2 files.

    • 1-new_failing_test.diff adds a new test to demonstrate the bug.
    • 2-fix-bug.diff fixes the bug, gets the test to pass. Maybe someone can think of a better fix, but I think this one is good enough.

    May 11th, 2010 @ 07:24 PM

  12. José Valim
    José Valim
    • Tag changed from :include, activerecord, bugmash, eager_loading, patch to :include, activerecord, eager_loading, patch
    • State changed from new to verified
    • Assigned user changed from Jeremy Kemper to Pratik

    Assigning to Pratik since he knows better about ActiveRecord.

    May 15th, 2010 @ 03:35 PM

  13. Diego Algorta
    Diego Algorta
    • Tag changed from :include, activerecord, eager_loading, patch to :include, activerecord, bugmash, eager_loading, patch
    • Assigned user changed from Pratik to Jeremy Kemper

    I've attached a patch.

    As requested by rizwanreza on #railsbridge, I'm attaching a merged patch with both the test and the fix in just one commit. Rebased to latest master branch.

    May 15th, 2010 @ 03:42 PM

  14. Pratik
    Pratik

    Please use .except(:includes) instead.

    May 15th, 2010 @ 03:48 PM

  15. Rizwan Reza
    Rizwan Reza
    • Assigned user changed from Jeremy Kemper to Pratik

    May 15th, 2010 @ 03:49 PM

  16. Diego Algorta
    Diego Algorta

    I've attached a patch.

    This new patch has a way much simpler test and fix. Thx Pratik for pointing me to the right direction as the old patch and fix were based on the initial work for rails 2.3

    May 15th, 2010 @ 04:40 PM

  17. Repository
    Repository
    • State changed from verified to resolved

    (from [3436fdfc12d58925e3d981e0afa61084ea34736c]) Fix for get_ids when including a belongs_to association on a has_many association [#2896 state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com
    http://github.com/rails/rails/commit/3436fdfc12d58925e3d981e0afa610...

    May 15th, 2010 @ 04:59 PM

  18. Rizwan Reza
    Rizwan Reza
    • Tag changed from :include, activerecord, bugmash, eager_loading, patch to :include, activerecord, eager_loading, patch

    May 15th, 2010 @ 05:21 PM