Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keys must be strings or symbols

Setup:

Ruby on rails 3.2.2
Ruby 1.9.2
dataMapper 1.2.0
mongoid 2.4.8
mongoid_orderable 1.0.0

Getting keys must be strings or symbols error when using mongoid_orderable.

I think it has something to do with dataMapper which I am using besides mongid (porting data from anoher server with dataMapper)

I've notices collisions between dataMapper and mongoid, since they both extend Symbol with various methods, asc, desc etc. Could this be the same problem?

Here is a snippet from my stacktrace if it is any help:

keys must be strings or symbols
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/bson-1.6.2/lib/bson/bson_c.rb:24:in `serialize'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/bson-1.6.2/lib/bson/bson_c.rb:24:in `serialize'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mongo-1.6.2/lib/mongo/collection.rb:436:in `update'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mongoid-2.4.8/lib/mongoid/collections/master.rb:25:in `block in update'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mongoid-2.4.8/lib/mongoid/collections/retry.rb:29:in `retry_on_connection_failure'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mongoid-2.4.8/lib/mongoid/collections/master.rb:24:in `update'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mongoid-2.4.8/lib/mongoid/collection.rb:149:in `update'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mongoid_orderable-1.0.0/lib/mongoid_orderable/mongoid/contexts/mongo.rb:6:in `inc'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mongoid_orderable-1.0.0/lib/mongoid_orderable/mongoid/criteria.rb:1:in `inc'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mongoid_orderable-1.0.0/lib/mongoid/orderable.rb:125:in `apply_position'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mongoid_orderable-1.0.0/lib/mongoid/orderable.rb:85:in `add_to_list'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:419:in `block in _run__3008157942946527494__save__4591629889417243504__callbacks'
/Users/hmm/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:215:in `block in _conditional_callback_around_742'

Current implementation of mongoid_orderable is following:

class MyModel
  include Mongoid::Document
  include Mongoid::Orderable
  default_scope order_by(:position => :asc)
end

I'm pretty sure it is related to _mongoid_orderable_ based on stacktrace and the fact that it works if I comment out _mongoid_orderable_ related lines. Also filed an issue here

like image 686
Tim Brunsmo Avatar asked Apr 22 '12 21:04

Tim Brunsmo


1 Answers

Solved it.

It was, like i though, a collision with mongoid and DataMapper. Changing a few lines in mongoid_orderable solved it.

If anyone stumble upon this problem it is because both DataMapper and Mongoid is extending Symbol with operators like gte, gt, lt, lte etc.

To avoid this collission just use mongoids alternative syntax:

instead of:

order_by(:created_at.desc)

and

where(:type.ne => 'Class')

Use this:

order_by(:created_at => :desc)

and

where(:type => {'$ne' => 'Class'})
like image 121
Tim Brunsmo Avatar answered Sep 25 '22 21:09

Tim Brunsmo