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.

default_scope patch

#1381

Default model scoping patch. With this AR will be able to set default scoping for models. For example it will be easier to get default order.

Reported by Paweł Kondzior · November 15th, 2008 @ 09:42 PM

State: committed
Milestone: 2.x
Assigned to: DHH DHH
Importance: none

Activity

  1. Paweł Kondzior
  2. Paweł Kondzior
    Paweł Kondzior
    • Tag set to activerecord, patch, scope

    I've added more tests for scopes

    November 16th, 2008 @ 12:21 PM

  3. Paweł Kondzior
    Paweł Kondzior

    default_scope with named_scope syntaxt.

    class Client < ActiveRecord::Base default_scope :order => 'name DESC' end

    November 16th, 2008 @ 02:51 PM

  4. Paweł Kondzior
  5. Repository
    Repository
    • State changed from new to committed

    (from [ff594b2bc94ff2a942fe6ca05672387722dee686]) Added default_scope to Base [#1381 default_scope patch state:committed] (Paweł Kondzior) http://github.com/rails/rails/co...

    November 16th, 2008 @ 03:01 PM

  6. DHH
    DHH
    • State changed from committed to open

    This actually won't work with threads because:

        def scoped_methods #:nodoc:
          Thread.current[:"#{self}_scoped_methods"] ||= []
        end
    
    

    So we need to promote default_scope to a class var and change the ||= [] to ||= default_scope.dup initialize default_scope to [].

    November 16th, 2008 @ 03:37 PM

  7. Repository
    Repository
    • State changed from open to committed

    (from [ca23287b448c2e007a5c93e43e762a10e0007b7a]) Revert "Added default_scope to Base [#1381 default_scope patch state:committed] (Paweł Kondzior)" -- won't gel with threads.

    This reverts commit ff594b2bc94ff2a942fe6ca05672387722dee686. http://github.com/rails/rails/co...

    November 16th, 2008 @ 03:37 PM

  8. Paweł Kondzior
  9. Paweł Kondzior
    Paweł Kondzior

    cattr_accessor wont work, it will fail all because it will define default_scope for ActiveRecord::Base not for Model that inherts from it. So class var isn't solution here :(

    November 16th, 2008 @ 05:05 PM

  10. Repository
    Repository

    (from [2530d0eea8eaecd2c61f99225f050ff47973e9a0]) Added default_scope to Base [#1381 default_scope patch state:committed] (Paweł Kondzior) http://github.com/rails/rails/co...

    November 16th, 2008 @ 06:08 PM

  11. Tom Stuart
    Tom Stuart

    This doesn't work right in combination with other named scopes because :create is initialised to nil if :conditions isn't specified, causing a failure when with_scope tries to call merge on it. The attached patch changes the behaviour to initialise :create to {}, which fixes the problem. Test included.

    November 17th, 2008 @ 08:19 PM

  12. DHH
    DHH

    Committed the fix, Tom. Thanks.

    November 17th, 2008 @ 09:08 PM

  13. Daniel Luz
    Daniel Luz

    The documentation is slightly out of date. This is a quick fix.

    December 1st, 2008 @ 05:00 AM

  14. Michał Szajbe
    Michał Szajbe

    When default_scope's :conditions is something like { :field => ['val1', 'val2'] } then new record's field is initialized to "['val1', 'val2']" which is not desired behaviour I think. I am not sure how it should be initialized then, though.

    January 22nd, 2009 @ 01:38 PM