Rails、DataMapper 和JSON问题
我在使用 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 获取所有捕获。所有
...我得到
[#<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呢?预先非常感谢;-) 克里斯。
I've some trouble with a really simple Rails setup using DataMapper. This is my model:
class Capture
include DataMapper::Resource
property :id, Serial
property :identifier, String
property :caption, Text
end
Now I add a new capture in Rails console by:
Capture.create(:identifier => '12345', :caption => 'Foo bar foo')
If I try to get all captures by
Capture.all
... i get a
[#<Capture @id=1 @identifier="12345" @caption=<not loaded>>]
First question: what does the "not loaded" mean in this case? But the trouble I have is I cannot convert the result to JSON:
Capture.all.to_json
NoMethodError: undefined method `encode_json' for #<Capture @id=1 @identifier="12345" @caption=<not loaded>>
Is it a DM issue? How to encapsulate such a result into JSON? Many thanks in advance ;-) Chris.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个问题的答案:
未加载
意味着数据加载被延迟到实际需要时,因为 Text 属性默认是惰性的。 http://datamapper.org/articles/spotlight_on_laziness.htmlThe answer for the first question:
not loaded
means that data loading is delayed until actually needed because Text property is lazy by default. http://datamapper.org/articles/spotlight_on_laziness.html