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?
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.