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

Mongoid 5 chained scope wrong context

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Blocker - P1 Blocker - P1
    • 5.0.1
    • Affects Version/s: 5.0.0
    • Component/s: None
    • Labels:
      None
    • Environment:
      railties (4.2.0), mongoid 5.0.0, ruby 2.1.1p76

      Given I have the following code

      class User
       include Mongoid::Document
       scope :author, -> { where(author: true) }
      end
      
      class Article
       include Mongoid::Document
       scope :public, -> { where(public: true) }
       scope :authored, -> {
        author_ids = User.author.pluck(:id)
        where(:author_id.in => author_ids)
       } 
      end
      

      Article.authored.to_a is working as expected:

      {"find"=>"users", "filter"=>{"author"=>true}, "projection"=>{"_id"=>1}}
      {"find"=>"articles", "filter"=>{"author_id"=>{"$in"=>[]}}}
      

      But
      Article.public.authored.to_a is not working properly:

      {"find"=>"articles", "filter"=>{"public"=>true, "author"=>true}, "projection"=>{"_id"=>1}}
      {"find"=>"articles", "filter"=>{"public"=>true, "author_id"=>{"$in"=>[]}}}
      

      it turns out that

       scope :author, -> { where(author: true) } 

      is called on Article instead of User

       {"find"=>"articles", "filter"=>{"public"=>true, "author"=>true} 

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            sbounmy Stephane Bounmy
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: