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

Is it a problem with eager loading or with my code?

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.2.0
    • Affects Version/s: None
    • Component/s: None

      item.rb

      class Item
        include Mongoid::Document
      
        field :caption
        belongs_to :user
        has_and_belongs_to_many :topics
      end
      

      user.rb

       ruby
      class User
        include Mongoid::Document
      
        field :email
        has_many :items
      end
      

      topic.rb

       ruby
      class Topic
        include Mongoid::Document
        
        field :name
      end
      

      Since each item has many topics and belongs to a user

      When I want to load the items like this

       ruby
      @items = Item.all.includes(:user, :topics)
      

      and my view haml

      -@items.each do |item|
        Item user:
        =item.user.email
      
        Item Caption
        =item.caption
      
        Item topics
        -item.topics.each do |t|
          =t.name
      

      I have 5 items to load, and each item belongs to a user, also it has many topics but what made me feeling a problem is what i got in the console.

      please note that Topic has no association in return of Item model

      MONGODB aioha_app_development['system.namespaces'].find({})
      MONGODB aioha_app_development['users'].find({:_id=>BSON::ObjectId('4e4acd9e57a3c0fc7b000004')}).sort([[:created_at, :desc]])
      MONGODB aioha_app_development['system.namespaces'].find({})
      MONGODB aioha_app_development['items'].find({}, {:_id=>1})
      MONGODB aioha_app_development['items'].find({}, {:_type=>1, "user_id"=>1}).sort([[:created_at, :desc]])
      MONGODB aioha_app_development['users'].find({:_id=>{"$in"=>[BSON::ObjectId('4e4acd9e57a3c0fc7b000004'), BSON::ObjectId('4e4d646557a3c03bd5000094')]}}).sort([[:created_at, :desc]])
      MONGODB aioha_app_development['items'].find({}).sort([[:created_at, :desc]])
      MONGODB aioha_app_development['system.namespaces'].find({})
      MONGODB aioha_app_development['topics'].find({:_id=>{"$in"=>[BSON::ObjectId('4e4d64a257a3c03bd5000099'), BSON::ObjectId('4e4d64af57a3c03bd500009b'), BSON::ObjectId('4e4d80ed57a3c0435200000e')]}}).sort([[:created_at, :desc]])
      MONGODB aioha_app_development['topics'].find({:_id=>{"$in"=>[BSON::ObjectId('4e4ad56a57a3c027e9000005'), BSON::ObjectId('4e4d64a257a3c03bd5000099'), BSON::ObjectId('4e4d80ed57a3c0435200000e')]}}).sort([[:created_at, :desc]])
      MONGODB aioha_app_development['topics'].find({:_id=>{"$in"=>[BSON::ObjectId('4e4d64a257a3c03bd5000099'), BSON::ObjectId('4e4ad56a57a3c027e9000005'), BSON::ObjectId('4e4d383857a3c05417000005'), BSON::ObjectId('4e4d382c57a3c05417000003')]}}).sort([[:created_at, :desc]])
      MONGODB aioha_app_development['topics'].find({:_id=>{"$in"=>[BSON::ObjectId('4e4d64a257a3c03bd5000099'), BSON::ObjectId('4e4d64af57a3c03bd500009b')]}}).sort([[:created_at, :desc]])
      MONGODB aioha_app_development['topics'].find({:_id=>{"$in"=>[BSON::ObjectId('4e4ad56a57a3c027e9000005'), BSON::ObjectId('4e4d383857a3c05417000005'), BSON::ObjectId('4e4d382c57a3c05417000003')]}}).sort([[:created_at, :desc]])
      

      Any thoughts?

            Assignee:
            Unassigned Unassigned
            Reporter:
            amrnt Amr Tamimi
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: