Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5483

ActiveRecord parity: allow defining default_scope as a class method.

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: Query
    • None

      ActiveRecord allows defining default scope as a class method, while Mongoid does not. To be as compatible with AR as possible, we should provide this option in Mongoid.

      == Original Description ==
      Nothing major just wondered why Mongoid works differently here.

      At some Rails Performance Guide i read that scopes in general as well default_scope is just a macro that that is turning the given proc into a class method.

      So to save a few nanoseconds of work and improve readability one should just write them as class methods.

      This works for normal scopes but not for `default_scope`.

      default_scope -> {
        where(:created_at.gte => 1.week.ago)
      }
      # this should do the same as it does with ActiveRecord
      def self.default_scope
        where(:created_at.gte => 1.week.ago)
      end
      

            Assignee:
            dmitry.rybakov@mongodb.com Dmitry Rybakov
            Reporter:
            vitasek@tqsoft.de Ralf Vitasek
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: