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

HABTM#find Not Scoping

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

      When calling #find on a many to many relation, it's actually not performing any scoping at all, so simply if the id matches it will return the document.

      Example to reproduce:

      class AssetLibrary
        include Mongoid::Document
        has_and_belongs_to_many :writers, :class_name => 'User', :inverse_of => :writable_asset_libraries
        has_and_belongs_to_many :readers, :class_name => 'User', :inverse_of => :readable_asset_libraries
      end
      
      class User
        include Mongoid::Document
        has_and_belongs_to_many :writable_asset_libraries, :class_name => 'AssetLibrary', :inverse_of => :writers
        has_and_belongs_to_many :readable_asset_libraries, :class_name => 'AssetLibrary', :inverse_of => :readers
      end
      
      user = User.create
      writable = user.writable_asset_libraries.create
      readable = user.readable_asset_libraries.create
      
      user.writable_asset_libraries.find(readable.id)
      

      Note the find generates a MongoDB query of:

      MONGODB mongoid_sandbox['asset_libraries'].find({:_id=>BSON::ObjectId('4e85dda98ad548383e000003')}).limit(-1).sort([[:_id, :asc]])
      

      This should at least have the scope of the ids array from writable_asset_libraries_ids.

            Assignee:
            Unassigned Unassigned
            Reporter:
            durran Durran Jordan
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: