5

设置:

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

使用mongoid_orderablekeys must be strings or symbols时出错。

我认为这与我正在使用的数据映射器有关(使用数据映射器从另一个服务器移植数据)

我注意到 dataMapper 和 mongoid 之间存在冲突,因为它们都使用各种方法、asc、desc 等扩展 Symbol。这可能是同一个问题吗?

如果有帮助,这是我的堆栈跟踪中的一个片段:

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'

mongoid_orderable 的当前实现如下:

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

我很确定它基于堆栈跟踪与 _mongoid_orderable_ 相关,并且如果我注释掉 _mongoid_orderable_ 相关行,它就会起作用。还在这里提出了问题

4

1 回答 1

5

解决了。

就像我一样,它与 mongoid 和 DataMapper 发生了冲突。在mongoid_orderable中更改几行解决了它。

如果有人偶然发现这个问题,那是因为 DataMapper 和 Mongoid 都在使用诸如gte, gt, lt, lteetc之类的运算符扩展 Symbol。

为了避免这种冲突,只需使用 mongoids 替代语法:

代替:

order_by(:created_at.desc)

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

用这个:

order_by(:created_at => :desc)

where(:type => {'$ne' => 'Class'})
于 2012-04-23T10:49:58.253 回答