MongoMapper 不允许我创建对象

发布于 2024-08-26 10:17:08 字数 1792 浏览 7 评论 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 控制台:

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>'

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

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

I'm just learning MongoDB and MongoMapper. This is on Rails 3.

I created a blog in app/models/blog.rb:

class Blog
  include MongoMapper::Document

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

I go into the Rails console:

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>'

Am I overlooking something really dumb, or is this something in my setup?

I'm using the mongo_mapper version you get by adding it to your Gemfile, so I'm wondering if it might be that. I'd appreciate any suggestions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2024-09-02 10:17:08

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

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

As I suspected, this was a silly mistake. I was using Text instead of String, and Mongo doesn't natively support Text as a key. If you define a from_mongo method, it would.

Thanks to DanP on IRC for pointing out my error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文