【问题标题】:Rails, DataMapper & JSON issueRails、DataMapper 和 JSON 问题
【发布时间】:2011-05-08 01:21:24
【问题描述】:

我在使用 DataMapper 进行非常简单的 Rails 设置时遇到了一些麻烦。这是我的模型:

class Capture
  include DataMapper::Resource
  property :id, Serial
  property :identifier, String
  property :caption, Text
 end

现在我通过以下方式在 Rails 控制台中添加一个新的捕获:

Capture.create(:identifier => '12345', :caption => 'Foo bar foo')

如果我尝试通过

获取所有捕获

全部捕获

...我得到一个

[#<Capture @id=1 @identifier="12345" @caption=<not loaded>>]

第一个问题:在这种情况下,“未加载”是什么意思?但我遇到的问题是我无法将结果转换为 JSON:

Capture.all.to_json

NoMethodError: undefined method `encode_json' for #<Capture @id=1 @identifier="12345" @caption=<not loaded>>

是 DM 问题吗?如何将这样的结果封装成 JSON?非常感谢 ;-) 克里斯。

【问题讨论】:

  • 好的,找到了:要在 DataMapper 中使用 to_json,我需要 dm-serializer 作为 Gem。现在工作正常;-)
  • 你应该回答你自己的问题并接受它。
  • @Simon 说得对,不要在答案标题中添加[SOLVED],只需回答您自己的问题并接受即可。

标签: ruby-on-rails json ruby-datamapper


【解决方案1】:

第一个问题的答案:not loaded 表示数据加载延迟到实际需要,因为默认情况下 Text 属性是惰性的。 http://datamapper.org/articles/spotlight_on_laziness.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多