【发布时间】: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