This project is archived and is in readonly mode.

#6164 open
Joseph Palermo

Eager load of a model with a default_scope of "select" also appends "table".* to the query

Reported by Joseph Palermo | December 14th, 2010 @ 06:54 PM

class Thing < ActiveRecord::Base
has_many :posts end
class Post < ActiveRecord::Base
default_scope select(column_names - ['body']) end

Thing.includes(:posts).all does the following for the posts query:

SELECT id, fk_column_id, column1, etc, posts.* FROM posts WHERE (pages.fk_column>in IN(1,2,3))

It takes the default select scope, but also appends posts.* to the end of it.

This is caused by the :select find_option in the find_associated_records inside of association_preload.rb. This is what it is now:

:select => preload_options[:select] || options[:select] || Arel::SqlLiteral.new("#{table_name}.*")

The third || case is not needed, since that is the default for any select and it causes the above behavior.

Unless of course this is used to for some edge case behavior? I'm guessing not, since that parameter (or something like it) was in the first commit of association_preload.rb

I can do the test and patch. I wanted to check that this wasn't the desired behavior first though.

Comments and changes to this ticket

  • rails

    rails March 15th, 2011 @ 12:00 AM

    • State changed from “new” to “open”

    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.

  • rails

    rails March 15th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”
  • Joseph Palermo

    Joseph Palermo March 16th, 2011 @ 05:02 AM

    I just verified this behavior still exists in Rails 3. I'll create a test case and a fix in the next couple of days.

  • Joseph Palermo

    Joseph Palermo March 16th, 2011 @ 03:36 PM

    • Tag set to association, default_scope, patch

    Here is a patch with tests and the fix. Simply removing "|| table[Arel.star]" broke HABTM. So now it is only applied to HABTM eager loads.

  • Joseph Palermo

    Joseph Palermo March 22nd, 2011 @ 04:15 AM

    • State changed from “stale” to “open”

    Missed the re-open step.
    [state:open]

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages