I'm trying to use mongoid with Rails 4.1.0 app and am getting error 17287 on moongodb 2.6.0 (same as 10068 on earlier versions of mongodb). Here is the error message:

The operation: #<Moped::Protocol::Query @length=127 @request_id=5 @response_to=0 @op_code=2004 @flags=[] @full_collection_name="educandose_development.users" @skip=0 @limit=-1 @selector={"$query"=>{"_id"=>{"$oid"=>BSON::ObjectId('534d6f4f6372618443000000')}}, "$orderby"=>{:_id=>1}} @fields=nil> failed with error 17287: "Can't canonicalize query: BadValue unknown operator: $oid" See https://github.com/mongodb/mongo/blob/master/docs/errors.md for details about this error.

Any idea of what could be wrong?

有帮助吗?

解决方案

After looking for a while, I realized that the new json cookies serializer on rails 4.1 breaks moped queries on devise resources.

To fix that, remove the following line on the cookies_serializer.rb initializer

Rails.application.config.action_dispatch.cookies_serializer = :json

You may want to get the old sessions_store.rb file back with content similar to:

YourApp::Application.config.session_store :cookie_store, key: '_yourapp_session'

or try the master branch of devise.

Take a look here: https://github.com/plataformatec/devise/issues/2949#issuecomment-40520236 and here: https://github.com/plataformatec/devise/pull/2882

其他提示

Temporarily, until moped/session/json formatting is fixed, I'm using:

# app/models/concerns/zero_oid_fix.rb
module ZeroOidFix
  extend ActiveSupport::Concern

  module ClassMethods
    def serialize_from_session(key, salt)
      record = to_adapter.get((key[0]["$oid"] rescue nil))
      record if record && record.authenticatable_salt == salt
    end
  end
end

And in devise model:

class User

  devise :database_authenticatable, ...

  # NOTE: Has to be after devise
  include ZeroOidFix

  ...
end

Hope this answer will get obsolete fast.

  1. Comment out the line below from the cookies_serializer.rb

    Rails.application.config.action_dispatch.cookies_serializer = :json

  2. Delete cookies.

  3. Restart server.

Worked for me on "rails 4.1.4, devise 3.2.4, mongoid 4.0.0"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top