MongoMapper不会让我创建一个对象

时间:2010-03-28 03:46:07

标签: ruby-on-rails-3 mongomapper

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

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

class Blog
  include MongoMapper::Document

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

我进入Rails控制台:

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版本,所以我想知道它是否可能。我很感激任何建议!

1 个答案:

答案 0 :(得分:0)

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

感谢DanC在IRC上指出我的错误。