title: Restkit 0.20: POST CoreData relationship with Foreign-Key
body: <p>I'm struggling a bit with RestKit and CoreData, especially since there are so little examples and documentation out there for RestKit 0.20.</p>
  
  <p>I have an (managed) object <code>Song</code> with a many-to-one relationship with <code>Album</code>. The following code can post JSON, but not in the <em>flattened</em> format, which the server excepts.</p>
  
  <pre><code>// Defined elsewhere
  Album *theAlbum;
  RKObjectManager *objMan = [self objectManager];
  
  // Response Mapping
  RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Song class]];
  [responseMapping addAttributeMappingsFromDictionary:@{ @"song": @"songID" }];
  NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
  RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                     pathPattern:@"/api/song"
                                                                                         keyPath:nil
                                                                                     statusCodes:statusCodes];
  
  // Request Mapping
  RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
  RKEntityMapping *albumRelationshipMapping = [RKEntityMapping mappingForEntityForName:@"Album" inManagedObjectStore:[objMan managedObjectStore]];
  [albumRelationshipMapping addAttributeMappingsFromDictionary:@{@"id": @"albumID", }];
  [requestMapping addAttributeMappingsFromDictionary:@{ @"title": @"title", @"length": @"length" }];
  [requestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"album"
                                                                                 toKeyPath:@"album"
                                                                               withMapping:albumRelationshipMapping]];
  requestMapping = [requestMapping inverseMapping];
  RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[Song class] rootKeyPath:nil];
  
  [objMan addRequestDescriptor:requestDescriptor];
  [objMan addResponseDescriptor:responseDescriptor];
  
  // Create a new temporary song object
  Song *song = [NSEntityDescription insertNewObjectForEntityForName:@"Song"
                                                  inManagedObjectContext:[[objMan managedObjectStore] mainQueueManagedObjectContext]];
  song.title = @"Some Title";
  song.length = 123;
  song.album = theAlbum;
  
  // Post operation
  objMan.requestSerializationMIMEType = RKMIMETypeJSON;
  RKManagedObjectRequestOperation *operation = [objMan appropriateObjectRequestOperationWithObject:song
                                                                                            method:RKRequestMethodPOST
                                                                                              path:@"/api/song"
                                                                                        parameters:nil];
  [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
      // Success
  } failure:^(RKObjectRequestOperation *operation, NSError *error) {
      // Failure
  }];
  [objMan enqueueObjectRequestOperation:operation];
  </code></pre>
  
  <p>This code will post a JSON body like this:</p>
  
  <p><code>{"title":"Some Title","length":"123","album":{"id":"6e32ae476815f365"}}</code></p>
  
  <p>However, the server expects a JSON body like this:</p>
  
  <p><code>{"title":"Some Title","length":"123","album":"6e32ae476815f365"}</code></p>
  
  <p>I.e. the relationship <code>album</code> should be a <em>flattened</em> foreign key instead of a nested object. But when I try to alter the <code>albumRelationshipMapping</code> like this:</p>
  
  <pre><code>[albumRelationshipMapping setIdentificationAttributes:@[ @"albumID" ]];
  [albumRelationshipMapping addAttributeMappingToKeyOfRepresentationFromAttribute:@"albumID"];
  </code></pre>
  
  <p>it throws an exception.
  (<code>NSInvalidArgumentException', reason: '*** -[NSProxy doesNotRecognizeSelector:allKeys] called!'</code>)</p>
  
  <p>Anybody knows what I'm doing wrong here? Or is there example code out there, which can steer me in the right direction?</p>
  
  <p>Sorry if this question was already answered somewhere else. I searched all of stackoverflow and the google groups, but couldn't find a specific solution for my case (RestKit 0.20, CoreData, relationship with just a FK).</p>
  
  <p>Thanks, Dirk</p>