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

'includes' doesn't work on paranoid has_many relations.

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

      Mongoid version 3.1.4.

      This is a continuation to Issue MONGOID-2916

      The same case doesn't work if the included model is paranoid, so:

      class Band
      include Mongoid::Document
      has_many :albums
      end

      class Album
      include Mongoid::Document
      include Mongoid::Paranoia
      belongs_to :band
      field :name
      end

      Album.create(band_id: Band.create.id)
      Album.create(band_id: Band.create.id)

      And then did this with logging:

      Band.includes(:albums).each do |band|
      p band.albums.first.name # Does not hit the database again.
      end

      MOPED: 127.0.0.1:27017 QUERY database=gocheap_development collection=bands selector={} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.6180ms)
      MOPED: 127.0.0.1:27017 QUERY database=gocheap_development collection=albums selector={"deleted_at"=>nil, "band_id"=>{"$in"=>["519a1df35b9e05cc01000001", "519a1df35b9e05cc01000003"]}} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.6425ms)
      MOPED: 127.0.0.1:27017 QUERY database=gocheap_development collection=albums selector={"$query"=>

      {"deleted_at"=>nil, "band_id"=>"519a1df35b9e05cc01000001"}

      , "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.6695ms)
      MOPED: 127.0.0.1:27017 QUERY database=gocheap_development collection=albums selector={"$query"=>

      {"deleted_at"=>nil, "band_id"=>"519a1df35b9e05cc01000003"}

      , "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.6542ms)

      So it does hit the database again.

            Assignee:
            Unassigned Unassigned
            Reporter:
            exoth Yuriy Trofimenko
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: