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.

find with :include ignores default_scope

#2931


class Game < AR::B
has_many :articles end
class Article < AR::B
belongs_to :game default_scope :order => 'created_at DESC' end

Calling Game.first(:include => :articles) generate this SQL


SELECT * FROM
games LIMIT 1
SELECT articles.* FROM articles WHERE (articles.game_id = 1)

When it should be

SELECT * FROM
games LIMIT 1
SELECT articles.* FROM articles WHERE (articles.game_id = 1) ORDER BY created_at DESC

Reported by rubymood · July 21st, 2009 @ 10:03 AM

State: resolved
Milestone: 3.x
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Deleted User
    Deleted User

    I can confirm this in Rails 2.3.3

    September 29th, 2009 @ 08:40 PM

  2. John
    John

    I can confirm this as of 2.3.4.

    November 12th, 2009 @ 12:06 AM

  3. Jeff
    Jeff

    Oh yeah, this is something I encountered when I was developing is_paranoid. preload_associations calls with_exclusive_scope which murders the default_scope on :includes

    It seems default_scope's intention and (er...) scope could stand to be more explicit. A decent number of people certainly expect it to be more default than it seems to be.

    November 12th, 2009 @ 12:10 AM

  4. Evan
    Evan

    I can confirm in Rails 2.3.5

    And agree the behavior is unexpected.

    class Book
    default_scope :conditions => "visible = true" end

    class Author
    has_many :books end

    I would expect the following two cases to return the same books:

    applies the default scope when not eager loading (will only have visible books)

    author = Author.first
    author.books

    does not apply the default scope when eager loading (will have both visible and non visible books)

    author = Author.first(:includes => :books)
    author.books

    February 11th, 2010 @ 10:47 PM

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

    May 4th, 2010 @ 06:48 PM

  6. Lawrence Curtis
    Lawrence Curtis
    • Tag changed from 2.x, :include, active_record, default_scope to arel rails3, 2.x, 3, :include, active_record, default_scope

    This is still an issue in rails3, it's REALLY annoying, as i have no way of specifying options to included objects

    +1

    June 20th, 2010 @ 04:57 PM

  7. Lawrence Curtis
    Lawrence Curtis

    if somebody could point me in the right direction im more than happy to write a patch, might need a little help with the tests etc

    June 20th, 2010 @ 05:13 PM

  8. Subba
    Subba
    • Importance changed from to

    Hi i found the fix for this problem.
    eager loaded includes final call this method.

    find_associated_records in association_preload.rb

    this method calls

     reflection.klass.unscoped.apply_finder_options(find_options).to_a
    

    it should use scoped which uses default_scope (unscope ignores default scope).

    i am attaching patch with tests for rails 3.

    July 16th, 2010 @ 06:49 PM

  9. Subba
    Subba
    • Assigned user set to Neeraj Singh

    July 16th, 2010 @ 06:49 PM

  10. Subba
    Subba

    updating patch after rebasing with recent changes

    July 20th, 2010 @ 02:29 AM

  11. Neeraj Singh
    Neeraj Singh
    • Assigned user changed from Neeraj Singh to José Valim
    • State changed from new to open
    • Importance changed from to Low

    looks good

    +1

    July 20th, 2010 @ 03:50 AM

  12. Subba
    Subba

    rubymod can you verify this patch

    July 21st, 2010 @ 03:54 AM

  13. Repository
    Repository
    • State changed from open to resolved

    July 21st, 2010 @ 02:08 PM