正在尝试为密钥名为“buy only”的文档设置MongoMapper对象当我尝试启动应用程序时,我得到了这个

(eval):1: syntax error, unexpected '-', expecting ';' or '\n'
          def buy-only       ^
(eval):3: syntax error, unexpected keyword_end, expecting end-of-input
.rvm/gems/ruby-2.0.0-p0/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/keys.rb:88:in `module_eval'
.rvm/gems/ruby-2.0.0-p0/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/keys.rb:88:in `create_accessors_for'
.rvm/gems/ruby-2.0.0-p0/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/keys.rb:27:in `block in key'
.rvm/gems/ruby-2.0.0-p0/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/keys.rb:25:in `tap'
.rvm/gems/ruby-2.0.0-p0/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/keys.rb:25:in `key'
.rvm/gems/ruby-2.0.0-p0/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/protected.rb:20:in `key'

以下是我对模型中键的定义:
key :'buy-only', Boolean,

似乎mongomapper试图自动生成一个函数来处理key buy only,由于连字符不是有效语法,所以它失败了。这个错误有什么解决办法吗?请不要质疑Mongo属性名中连字符的使用如果我能改变这一点,我就不会问了。

最佳答案

MongoMapper目前无法正确地支持hypens的键,因为它将尝试为那些键创建访问器方法,而且buy-only在Ruby中不是有效的方法名。
不过,我有一个分支,希望很快会合并到master,其中包括键别名这会让你做如下事情:

key :buy_only, :alias => "buy-only"

你可以在这里找到分行:https://github.com/cheald/mongomapper/tree/alias_keys

关于ruby - 连字符键名称上的MongoMapper语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17385384/

10-08 23:09