You need to use the RKRelationshipMapping
class and the relationshipMappingFromKeyPath:toKeyPath:withMapping:
method to create the relationship and then set it with addPropertyMapping:
.
Add Restkit relationship mapping with key path different than relationship name
-
04-07-2023 - |
Question
I'm attempting to add a relationship mapping to one of my request mappings.
The POST request should send body content as such:
{"account":{"key":"the_key_goes_here","terms_of_service":1,"person_attributes":{"email":"test@example.com"}}}
I have setup the account mapping to be:
NSMutableDictionary *accountAttributes = [NSMutableDictionary dictionaryWithDictionary: @{@"key":@"key",@"terms_of_service":@"terms_of_service",@"source":@"source"}];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:accountAttributes];
Now, in an attempt to add the person_attributes to the request, I do the following:
NSMutableDictionary *userAttributes = [NSMutableDictionary dictionaryWithDictionary: @{@"first_name":@"first_name",@"last_name":@"last_name",@"email":@"email"}];
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
[userMapping addAttributeMappingsFromDictionary:userAttributes];
[requestMapping addRelationshipMappingWithSourceKeyPath:@"person" mapping:userMapping];
However, this always sends as:
{"account":{"key":"the_key_goes_here","terms_of_service":1,"person":{"email":"test@example.com"}}}
In an attempt to change person to person_attributes I modify the relationshipmappingwithsourcekeypath to:
[requestMapping addRelationshipMappingWithSourceKeyPath:@"person_attributes" mapping:userMapping];
But, it fails with an error every time, stating: the entity Account is not key value coding-compliant for the key "person_attributes"
I understand this is because the Account
doesn't have a person_attributes
value. Is there a way to add the keypath as person
but have it send as person_attributes
?
Thanks for your help in advance!
Solution