键必须是字符串或符号

时间:2012-04-22 21:54:13

标签: ruby-on-rails mongoid datamapper

设定:

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

使用mongoid_orderable时出现keys must be strings or symbols错误。

我认为这与我使用的dataMapper有关,除了mongid(使用dataMapper移植来自其他服务器的数据)

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

如果有任何帮助,这是我的stacktrace的一个片段:

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_相关的行,它可以工作。还提交了一个问题here

1 个答案:

答案 0 :(得分:5)

解决了它。

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

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

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

而不是:

order_by(:created_at.desc)

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

使用此:

order_by(:created_at => :desc)

where(:type => {'$ne' => 'Class'})