8

In Rails 3.0.8 the json contains a root element with your model name. For example my Location model.

[
{
location: {
city: San Diego
name: Mission Valley YMCA Krause Family Skatepark
pads_required: 0
country: United States

And the mapping provider looked directly for the location object.

RKObjectMapping* locationMapping = [RKObjectMapping mappingForClass:[RKLocation class]];   
[locationMapping mapKeyPath:@"id" toAttribute:@"locationId"];
...
[objectManager.mappingProvider setMapping:locationMapping forKeyPath:@"location"];

Now when you upgrade to rails 3.1.0 the root node "location" is now removed by default and I'm not sure how to configure the mapping provider without it? I tried nil and looked for alternative methods but was unsuccessful.

Do you know how to map this? Please help!

[
{
   city: San Diego
   name: Mission Valley YMCA Krause Family Skatepark
   pads_required: 0
   country: United States
jspooner
  • 10,975
  • 11
  • 58
  • 81

3 Answers3

7

From the RestKit side, I don't know, but from this topic it looks like you can get the json back to what RestKit expects by doing:

class Location < ActiveRecord::Base
  self.include_root_in_json = true
end

Edit: For completeness, here's how you'd do it with RestKit:

RKObjectMapping* locationMapping = [RKObjectMapping mappingForClass:[RKLocation class]];   
[locationMapping mapKeyPath:@"id" toAttribute:@"locationId"];
...
[objectManager.mappingProvider addObjectMapping:locationMapping];

And then calling the mapper later:

RKObjectMapping* locationMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[RKLocation class]];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/locations" objectMapping:locationMapping delegate:self];

And then you'd handle the objects in RKObjectLoader delegate methods.

Community
  • 1
  • 1
Evan Cordell
  • 4,108
  • 2
  • 31
  • 47
4

In RestKit, you can register a mapping to contain a root model name like this:

[objectManager.mappingProvider registerMapping:locationMapping withRootKeyPath:@"location"];
tassock
  • 1,633
  • 1
  • 17
  • 32
0

Short and sweet answer: forKeyPath:@"" will work.

Simon Woodside
  • 7,175
  • 5
  • 50
  • 66