带连字符的键名称的MongoMapper语法错误

时间:2013-06-29 22:44:32

标签: ruby mongodb mongomapper

尝试使用名为' buy-only'的密钥为Document设置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试图自动生成一个函数来处理密钥只购买,并且因为连字符不是有效的语法,所以它失败了。这个错误有没有解决方法?请不要在Mongo的属性名称中使用连字符。如果我可以改变它,我就不会问。

1 个答案:

答案 0 :(得分:0)

MongoMapper当前不会正确地支持超量密钥,因为它会尝试为这些密钥创建访问器方法,而buy-only不是Ruby中的有效方法名。

但是,我有一个分支,希望很快就会合并到master,其中包括键别名。这可以让你做类似的事情:

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

您可以在此处找到分支:https://github.com/cheald/mongomapper/tree/alias_keys