Took longer than I thought to get a basic use case that fails. Scenario 2 below is the failure:
My Code:
class User < ActiveResource::Base
self.site = “http://localhost:9000”
end
class Image < ActiveResource::Base
end
class ImagePage < ActiveResource::Base
self.element_name = “image_page”
self.collection_name = “irrelevant_never_used_in_this_manner”
end
class ApplicationController < ActionController::Base
def index
Dir.glob(“#{Rails.root}/app/models/*.rb”).sort.each { |file| require_dependency file }
user = User.find(123)
p user
render :text => “hello world”
end
end
Scenario 1 using remove_root in decode, user.json has “id”
def decode(json)
Formats.remove_root(ActiveSupport::JSON.decode(json))
end
GET /users/123.json
“{"id":"123","image_page":{"images":[{"id":123},{"id":456}],"total":2000,"count":2,"start_index":0}}”
p user = #<User:0x00000103ab6c98 @attributes={“id”=>“123”, “image_page”=>#<ImagePage:0x00000103ab5cd0 @attributes={“images”=>[#<Image:0x00000103aac158 @attributes={“id”=>123}, @prefix_options={}, @persisted=false>, #<Image:0x00000103aabed8 @attributes={“id”=>456}, @prefix_options={}, @persisted=false>], “total”=>2000, “count”=>2, “start_index”=>0}, @prefix_options={}, @persisted=false>}, @prefix_options={}, @persisted=true>
Scenario 2 using remove_root in decode, user.json does not have “id”
def decode(json)
Formats.remove_root(ActiveSupport::JSON.decode(json))
end
GET /users/123.json
“{"image_page":{"images":[{"id":123},{"id":456}],"total":2000,"count":2,"start_index":0}}”
p user = #<User:0x00000101133cb8 @attributes={“images”=>[#<Image:0x000001011327a0 @attributes={“id”=>123}, @prefix_options={}, @persisted=false>, #<Image:0x00000103a49e40 @attributes={“id”=>456}, @prefix_options={}, @persisted=false>], “total”=>2000, “count”=>2, “start_index”=>0}, @prefix_options={}, @persisted=true>
Scenario 3 not using remove_root in decode, user.json has “id”
def decode(json)
ActiveSupport::JSON.decode(json)
end
GET /users/123.json
“{"id":"123","image_page":{"images":[{"id":123},{"id":456}],"total":2000,"count":2,"start_index":0}}”
p user = #<User:0x0000010455a168 @attributes={“id”=>“123”, “image_page”=>#<ImagePage:0x00000104559880 @attributes={“images”=>[#<Image:0x00000104557c88 @attributes={“id”=>123}, @prefix_options={}, @persisted=false>, #<Image:0x00000104557a08 @attributes={“id”=>456}, @prefix_options={}, @persisted=false>], “total”=>2000, “count”=>2, “start_index”=>0}, @prefix_options={}, @persisted=false>}, @prefix_options={}, @persisted=true>
Scenario 4 not using remove_root in decode, user.json does not have “id”
def decode(json)
ActiveSupport::JSON.decode(json)
end
GET /users/123.json
“{"image_page":{"images":[{"id":123},{"id":456}],"total":2000,"count":2,"start_index":0}}”
p user = #<User:0x000001044166d0 @attributes={“image_page”=>#<ImagePage:0x00000104415f28 @attributes={“images”=>[#<Image:0x00000104414920 @attributes={“id”=>123}, @prefix_options={}, @persisted=false>, #<Image:0x000001044146a0 @attributes={“id”=>456}, @prefix_options={}, @persisted=false>], “total”=>2000, “count”=>2, “start_index”=>0}, @prefix_options={}, @persisted=false>}, @prefix_options={}, @persisted=true>