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

Dynamic scope example in tutorial uses wrong syntax

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 7.1.4, 7.0.10
    • Affects Version/s: None
    • Component/s: Docs
    • None

      The scope needs to contain a proc in order for it to be set correctly. The docs contain the following code snippet: 

       

      class Browser < Canvas 
       field :version, type: Integer 
       scope :recent, where(:version.gt => 3)
      end

       

      which yields the following error:

       

      message:
        Defining a scope of value #<Mongoid::Criteria:0x00007face0236ba0> on Browser is not allowed.
      summary:
        Scopes in Mongoid must be procs that wrap criteria objects.
      resolution:
        Change the scope to be a proc wrapped critera. Example:
         class Band
           include Mongoid::Document
           scope :inactive, ->{ where(active: false) }
         end
      
      

      The suggested fix is as follows: 

       

      class Browser < Canvas    
        field :version, type: Integer    
        scope :recent, ->{ where(:version.gt => 3) }
      end
      

       

       

       

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: