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

Is `includes` working properly here?

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • 3.1.6
    • Affects Version/s: None
    • Component/s: None

      Hi, Guys

      I am working on a rails app using mongoid, and includes seems not working properly to me. I on a Mac Lion and ruby 1.9.2p0. The mongoid gem version is 2.4.1.

      [update] I also have identity_map_enabled set to true in mongoid.yml

      I have an Offer class that looks like this:

      class Offer
        include Mongoid::Document
        include Mongoid::Timestamps
      
        has_many :messages
      end
      

      and a Message class that looks like this:

      class Message
        include Mongoid::Document
        include Mongoid::Timestamps
      
        belongs_to :offer
      end
      

      And here is the output from rails console

      ruby-1.9.2-p0 :010 > Offer.limit(10).includes(:messages).each {|o| o.messages.entries }
      MONGODB [DEBUG] Cursor#close 8323320496372318445
      MONGODB kass_development['offers'].find({}).limit(10)
      MONGODB kass_development['system.namespaces'].find({})
      MONGODB kass_development['messages'].find({"offer_id"=>{"$in"=>[BSON::ObjectId('4f28ea6dcf60210bf8000002'), BSON::ObjectId('4f28eaadcf60210bf8000003'), BSON::ObjectId('4f28eb14cf60210bf8000004'), BSON::ObjectId('4f2ee439cf60210168000006'), BSON::ObjectId('4f2ee65acf60210168000007'), BSON::ObjectId('4f2f3550cf60211f38000005'), BSON::ObjectId('4f36beebcf6021039c000003'), BSON::ObjectId('4f36bfd8cf6021039c000004'), BSON::ObjectId('4f36c025cf6021039c000006'), BSON::ObjectId('4f2983dacf60210167000005')]}}).sort([[:created_at, :asc]])
      MONGODB kass_development['messages'].find({"offer_id"=>BSON::ObjectId('4f28ea6dcf60210bf8000002')}).sort([[:created_at, :asc]])
      MONGODB kass_development['messages'].find({"offer_id"=>BSON::ObjectId('4f28eaadcf60210bf8000003')}).sort([[:created_at, :asc]])
      MONGODB kass_development['messages'].find({"offer_id"=>BSON::ObjectId('4f28eb14cf60210bf8000004')}).sort([[:created_at, :asc]])
      MONGODB kass_development['messages'].find({"offer_id"=>BSON::ObjectId('4f36beebcf6021039c000003')}).sort([[:created_at, :asc]])
      MONGODB kass_development['messages'].find({"offer_id"=>BSON::ObjectId('4f36bfd8cf6021039c000004')}).sort([[:created_at, :asc]])
      MONGODB kass_development['messages'].find({"offer_id"=>BSON::ObjectId('4f36c025cf6021039c000006')}).sort([[:created_at, :asc]])
      MONGODB kass_development['messages'].find({"offer_id"=>BSON::ObjectId('4f2983dacf60210167000005')}).sort([[:created_at, :asc]])
       => #<Mongoid::Criteria
        selector: {},
        options:  {:limit=>10},
        class:    Offer,
        embedded: false>
      

      Notice it still makes separate queries for the messages for some of the offers.

      I googled for a while and could not find anything that helps.

      Thanks for any help in advance.

      Lin

            Assignee:
            durran.jordan@mongodb.com Durran Jordan
            Reporter:
            he9lin he9lin
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: