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

Class methods and scopes fail to query other models

    • Type: Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Priority: Icon: Critical - P2 Critical - P2
    • 5.0.2
    • Affects Version/s: 5.0.1, 5.0.0
    • Component/s: None

      Mongoid 5.0.0
      class Product
      scope :phyical_goods_scope

      { where(category_id: Category.where(name: "Phyical Goods").last.id }

      def self.physical_goods
      category_id = Category.where(name: "Physical Goods").last.id
      all.where(category_id: category_id)
      end
      end

      In this example, both the scope and the class method will generate the following:
      {"find"=>"products", "filter"=>{"name"=>"Physical Goods"}}

      It seems that any query done within a class method queries the class you are within regardless if you specify a different constant. In this example I'm trying to query for a specific Category and the name of the category instead gets associated with the constant Product.

      In Mongoid 5.0.1, it just throws
      NoMethodError (undefined method `with_default_scope' for {}:Hash)

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            superjova Paul Jovanovic
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: