工厂女孩 to_json 问题
当我对 Factory Girl 对象调用 to_json 时,出现 nil.[] 错误。
我在 Rails 3 上使用 DataMapper,当我在真实的 DataMapper 对象上调用 to_json 时,它工作得很好。知道工厂女孩对象失败的原因吗?
Factory.define :something do |b|
b.date_foo { 3.days.ago - 4.hours }
b.date_bar { 3.days.ago - 4.hours }
b.somestring '11'
b.anotherstring 'ASTRINGYAY'
b.thirdstring 'STRINGG'
b.fourthstring 'STRING'
b.otherstring 'thestring'
b.longerstring 'The string that is longer'
b.somenumber 3
b.someothernumber 2
b.an_id 5593
b.a_version '1.2.2.2'
b.a_string '1.2.2.2'
b.something '1.2.2.2'
b.code 'SOME CODE'
b.other_code 'API'
b.something_id 49
b.something_version 'OMG'
b.otherthing 'N/A'
b.lastthing 'A'
end
另外,我发现以下内容:
factory_girl_object.to_json
工作正常。
[factory_girl_object].to_json
失败。
DataMapper 显然使用 DataMapper::Collection 对象而不是数组,它处理 to_json 的内容与数组的处理有点不同。然而,我似乎无法弄清楚如何创建 DataMapper::Collection 对象来保存我的 Factory_Girl 对象。
想法?
When I call to_json on my Factory Girl object, I get a nil.[] error.
I'm using DataMapper on Rails 3, and when I call to_json on a real DataMapper object, it works just fine. Any idea why the Factory Girl object is failing?
Factory.define :something do |b|
b.date_foo { 3.days.ago - 4.hours }
b.date_bar { 3.days.ago - 4.hours }
b.somestring '11'
b.anotherstring 'ASTRINGYAY'
b.thirdstring 'STRINGG'
b.fourthstring 'STRING'
b.otherstring 'thestring'
b.longerstring 'The string that is longer'
b.somenumber 3
b.someothernumber 2
b.an_id 5593
b.a_version '1.2.2.2'
b.a_string '1.2.2.2'
b.something '1.2.2.2'
b.code 'SOME CODE'
b.other_code 'API'
b.something_id 49
b.something_version 'OMG'
b.otherthing 'N/A'
b.lastthing 'A'
end
Also, I discovered the following:
factory_girl_object.to_json
works fine.
[factory_girl_object].to_json
fails.
DataMapper apparently uses DataMapper::Collection objects instead of arrays, which handle to_json a bit different for it's contents than arrays do. I can't seem to figure out how to create a DataMapper::Collection object to hold my Factory_Girl objects, however.
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哈,还是回来回答一下吧。
DataMapper 以专门的类似数组的数据结构提供对象,为您处理 JSON 序列化。如果您尝试将 DM 对象放入常规数组中,它会严重失败,从而产生我上面提到的错误。
Ha, might as well come back and answer this.
DataMapper serves objects in a specialized array-like data structure that handles JSON serialization for you. If you try to put DM objects in a regular array, it fails horribly, spawning the errors I mentioned above.