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

Using find() in many to many relation raises Mongoid::Errors::DocumentNotFound even when the document is there

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

      Here's the rough sketch of the models:

      User Model:

      class User
        include Mongoid::Document
        has_and_belongs_to_many :projects
      end
      

      Project Model

      class Project
        include Mongoid::Document
        has_and_belongs_to_many :users
        field :name, :type => String
      end
      

      Here's what I encountered in the console:

      1.9.3p0 :005 > User.first.projects.first
       => #<Project _id: 4f6ac24aa902431e23000001, _type: nil, user_ids: [BSON::ObjectId('4f6ac24aa902431e2300000a'), BSON::ObjectId('4f6ac24aa902431e2300000b')], name: "Seed Project"> 
      
      1.9.3p0 :006 > Project.first
       => #<Project _id: 4f6ac24aa902431e23000001, _type: nil, user_ids: [BSON::ObjectId('4f6ac24aa902431e2300000a'), BSON::ObjectId('4f6ac24aa902431e2300000b')], name: "Seed Project"> 
      
      1.9.3p0 :007 > Project.find(Project.first.id)
       => #<Project _id: 4f6ac24aa902431e23000001, _type: nil, user_ids: [BSON::ObjectId('4f6ac24aa902431e2300000a'), BSON::ObjectId('4f6ac24aa902431e2300000b')], name: "Seed Project"> 
      
      1.9.3p0 :008 > User.first.projects.find(Project.first.id)
      Mongoid::Errors::DocumentNotFound: Document not found for class Project with id(s) 4f6ac24aa902431e23000001.
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/mongoid-2.4.6/lib/mongoid/criterion/inclusion.rb:151:in `block in execute_or_raise'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/mongoid-2.4.6/lib/mongoid/criterion/inclusion.rb:149:in `tap'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/mongoid-2.4.6/lib/mongoid/criterion/inclusion.rb:149:in `execute_or_raise'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/mongoid-2.4.6/lib/mongoid/criterion/inclusion.rb:129:in `find'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/mongoid-2.4.6/lib/mongoid/relations/referenced/many.rb:249:in `find'
      	from (irb):8
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/railties-3.2.2/lib/rails/commands/console.rb:47:in `start'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in `start'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/railties-3.2.2/lib/rails/commands.rb:41:in `<top (required)>'
      	from script/rails:6:in `require'
      	from script/rails:6:in `<main>'
      

      And yet...

      1.9.3p0 :09 > User.first.projects.first == Project.find(Project.first.id)
       => true 
      

      Aside from passing the BSON::ObjectId, I also tried passing the normal string id but the result is the same:

      1.9.3p0 :10 > Project.find("4f6ac24aa902431e23000001")
       => #<Project _id: 4f6ac24aa902431e23000001, _type: nil, user_ids: [BSON::ObjectId('4f6ac24aa902431e2300000a'), BSON::ObjectId('4f6ac24aa902431e2300000b')], name: "Seed Project"> 
      
      1.9.3p0 :11 > User.first.projects.find("4f6ac24aa902431e23000001")
      Mongoid::Errors::DocumentNotFound: Document not found for class Project with id(s) 4f6ac24aa902431e23000001.
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/mongoid-2.4.6/lib/mongoid/criterion/inclusion.rb:151:in `block in execute_or_raise'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/mongoid-2.4.6/lib/mongoid/criterion/inclusion.rb:149:in `tap'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/mongoid-2.4.6/lib/mongoid/criterion/inclusion.rb:149:in `execute_or_raise'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/mongoid-2.4.6/lib/mongoid/criterion/inclusion.rb:129:in `find'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/mongoid-2.4.6/lib/mongoid/relations/referenced/many.rb:249:in `find'
      	from (irb):10
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/railties-3.2.2/lib/rails/commands/console.rb:47:in `start'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in `start'
      	from /Users/marcrendlignacio/.rvm/gems/ruby-1.9.3-p0@storymapper/gems/railties-3.2.2/lib/rails/commands.rb:41:in `<top (required)>'
      	from script/rails:6:in `require'
      	from script/rails:6:in `<main>'
      

      Already executed rake db:mongoid:objectid_convert before this, but still receiving the same results.

      I don't know if this is a bug or I missed something else, but I kind of expect that it will do same behavior as Project.find would.

            Assignee:
            Unassigned Unassigned
            Reporter:
            padi padi
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: