0

当我在 Factory Girl 对象上调用 to_json 时,我得到一个 nil.[] 错误。

我在 Rails 3 上使用 DataMapper,当我在真实的 DataMapper 对象上调用 to_json 时,它工作得很好。知道为什么 Factory Girl 对象会失败吗?

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 对象。

想法?

4

1 回答 1

0

哈,还不如回来回答这个问题。

DataMapper 在一个专门的类数组数据结构中提供对象,为您处理 JSON 序列化。如果您尝试将 DM 对象放入常规数组中,它会严重失败,从而产生我上面提到的错误。

于 2011-01-26T02:47:53.740 回答