Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb error code 10068 or 17287 with rails 4.1 and devise

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?

like image 705
felipero Avatar asked Apr 15 '14 19:04

felipero


2 Answers

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

like image 181
felipero Avatar answered Sep 19 '22 03:09

felipero


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.

like image 34
Mirek Rusin Avatar answered Sep 20 '22 03:09

Mirek Rusin