Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestKit mapping with Rails 3.1

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
like image 586
jspooner Avatar asked Jun 28 '11 14:06

jspooner


2 Answers

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.

like image 133
Evan Cordell Avatar answered Nov 14 '22 10:11

Evan Cordell


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

[objectManager.mappingProvider registerMapping:locationMapping withRootKeyPath:@"location"];
like image 41
tassock Avatar answered Nov 14 '22 11:11

tassock