mongoid/mongoid — Issue #1478: has_and_belongs_to_many error: Mongo::OperationFailure: invalid query
Author: fbjork
Created: 2011-12-05 20:11:54+00:00
State: open

I have the following association:

class Account
has_and_belongs_to_many :followed_by, class_name: Account.to_s
end

running the following:
a = Account.new
a.followed_by

gives the error:
Mongo::OperationFailure: invalid query
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:101:in `next_document'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:248:in `each'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.3.3/lib/mongoid/cursor.rb:48:in `each'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.3.3/lib/mongoid/contexts/mongo.rb:212:in `iterate'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.3.3/lib/mongoid/criteria.rb:143:in `block in each'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.3.3/lib/mongoid/criteria.rb:143:in `tap'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.3.3/lib/mongoid/criteria.rb:143:in `each'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.3.3/lib/mongoid/relations/targets/enumerable.rb:153:in `each'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.3.3/lib/mongoid/relations/targets/enumerable.rb:223:in `entries'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.3.3/lib/mongoid/relations/targets/enumerable.rb:223:in `inspect'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.3.3/lib/mongoid/relations/referenced/many.rb:394:in `method_missing'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /Users/fredrik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.3/lib/rails/commands.rb:40:in `'
from script/rails:11:in `require'
from script/rails:11:in `

'

Also,

doing:
a.followed_by << Account.first

and then the following gives the same Mongo::OperationFailure: invalid query:
a.followed_by

doing a.reload works.