0

我只是在学习 MongoDB 和 MongoMapper。这是在 Rails 3 上。

我在 app/models/blog.rb 中创建了一个博客:

class Blog
  include MongoMapper::Document

  key :title, String, :required => true
  key :body, Text
  timestamps!
end

我进入 Rails 控制台:

导轨 c

Loading development environment (Rails 3.0.0.beta)
ruby-1.9.1-p378 > b = Blog.new
NoMethodError: undefined method `from_mongo' for Text:Module
    from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:323:in `get'
    from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:269:in `read_key'
    from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:224:in `[]'
    from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:7:in `block in inspect'
    from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:6:in `collect'
    from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:6:in `inspect'
    from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands/console.rb:47:in `start'
    from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands/console.rb:8:in `start'
    from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands.rb:34:in `<top (required)>'
    from /Users/jade/code/farmerjade/script/rails:10:in `require'
    from /Users/jade/code/farmerjade/script/rails:10:in `<main>'

我是否忽略了一些非常愚蠢的东西,或者这是我的设置中的东西?

我正在使用您通过将其添加到您的 Gemfile 中获得的 mongo_mapper 版本,所以我想知道是否可能是这样。我会很感激任何建议!

4

1 回答 1

0

正如我所怀疑的,这是一个愚蠢的错误。我使用的是 Text 而不是 String,而 Mongo 本身并不支持 Text 作为键。如果你定义一个 from_mongo 方法,它会。

感谢 IRC 上的 DanP 指出我的错误。

于 2010-03-28T04:39:18.450 回答