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.

Merge default scopes by default

#4583

I just co-authored a merged_default_scopes gem (AR plugin) with Brian Tatnall. It modifies the behavior of default_scope so that it merges the arguments of successive calls. We needed this in an app that was using a plugin that used default_scope and also needed to add to the default scope in the consuming model.

We think this should be the default behavior.

If you agree, we'd be glad to submit a patch, including a clear_default_scope method so that anybody counting on the current behavior to reset the default scope still has a way to accomplish that.

Reported by David Chelimsky · May 12th, 2010 @ 02:01 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. David Chelimsky
    David Chelimsky
    • Assigned user changed from José Valim to Yehuda Katz (wycats)

    May 12th, 2010 @ 02:01 PM

  2. David Chelimsky
    David Chelimsky
    • Tag set to activecord, default_scope
    • Assigned user changed from Yehuda Katz (wycats) to Pratik

    May 12th, 2010 @ 02:02 PM

  3. David Chelimsky
    David Chelimsky

    (sorry for the "Assigned user" spam - just changing that value to get this on the radar of more than one person)

    May 12th, 2010 @ 02:04 PM

  4. José Valim
    José Valim
    • Milestone cleared.
    • State changed from new to open
    • Assigned user changed from Pratik to José Valim

    Just discussed with Yehuda and Jeremy and we all agreed! Please do provide a patch! :)

    May 12th, 2010 @ 05:26 PM

  5. David Chelimsky
    David Chelimsky

    Excellent - will do so in the next day or so. Looks like it'll be a lot easier to do in rails-3 - nice job finding good abstractions.

    May 12th, 2010 @ 05:57 PM

  6. David Chelimsky
  7. Repository
    Repository
    • State changed from open to resolved

    (from [35a114a8941cb22d29a536f1215a23a8cf7c4756]) Modified default_scope to merge with any pre-existing default_scope and added AR::Base::clear_default_scope

    • clear_default_scope provides users who rely on the old behaviour of each call to default_scope overwriting any previous default scopes an opportunity to maintain that behaviour.

    [#4583 state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/35a114a8941cb22d29a536f1215a23...

    May 15th, 2010 @ 08:51 AM

  8. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM

  9. bterkuile
    bterkuile

    There is still an unsolved issue with this one:
    I would like:
    class Post
    default_scope order(:title) default_scope where(:state => 'active') end
    To be similar to:
    class Post
    default_scope order(:title).where(:state => 'active') end
    Since (Rails 3.0.1) activerecord-3.0.1/lib/active_record/base.rb line 890:
    Thread.current[key] = Thread.current[key].presence || self.default_scoping.dup
    seems to spoil stuff (.dup) I made an initializer with the following content:
    class ActiveRecord::Base
    def self.default_scope(options = {})

    key = :"#{self}_scoped_methods"
    Thread.current[key] = nil                                                                                                                                                                                                                
    self.default_scoping << construct_finder_arel(options, default_scoping.pop)
    

    end
    end
    Basically resetting Thread.current[key] when default_scope is invoked. This solves this issue, but there might be a better solution.

    November 2nd, 2010 @ 09:23 AM

  10. bterkuile
    bterkuile

    with better formatting:

    class ActiveRecord::Base
     def self.default_scope(options = {})
      key = :"#{self}_scoped_methods"
      Thread.current[key] = nil
      self.default_scoping << construct_finder_arel(options, default_scoping.pop)
     end
    end
    

    November 2nd, 2010 @ 09:28 AM