title: RestKit Object Mapping relationships with foreign keys
body: <p>Can RestKit connect a relationship without storing the foreign key as an attribute, i.e., directly from the keypath in the JSON?</p>
  
  <p>In particular, I've got a Job has_many Rooms relationship.  The room's JSON doesn't contain the job, rather, both are loaded separately:</p>
  
  <pre><code>- job: {
      id: 1,
      name: "John"
  }
  
  - room: {
      id: 4,
      job_id: 1,
      name: "spare bedroom"
  }
  </code></pre>
  
  <p>The Job is loaded before the Room.</p>
  
  <p>My CoreData models, Job has properties for </p>
  
  <pre><code>@interface Job : NSManagedObject
  @property (nonatomic, retain) NSNumber * identifier;
  @property (nonatomic, retain) NSString * name;
  @property (nonatomic, retain) NSSet *rooms;
  @end
  
  @interface Room : NSManagedObject
  @property (nonatomic, retain) NSNumber * identifier;
  @property (nonatomic, retain) NSString * name;
  @property (nonatomic, retain) Job *job;
  @end
  </code></pre>
  
  <p>Currently I add a <code>@property (nonatomic, strong) NSNumber *jobID;</code> to Room, which I <code>@synthesize</code>.</p>
  
  <pre><code>JobMapping:
      mapping = [RKManagedObjectMapping mappingForClass:[Job class]];
      [mapping setPrimaryKeyAttribute:@"identifier"];
  
      [mapping mapAttributes:@"name", nil];
      [mapping mapKeyPath:@"id" toAttribute:@"identifier"];
  
      [mapping mapRelationship:@"rooms" withMapping:[Room objectMapping]];
  
  
  
  RoomMapping
      mapping = [RKManagedObjectMapping mappingForClass:[Room class]];
      [mapping setPrimaryKeyAttribute:@"identifier"];
  
      [mapping mapAttributes:@"name", nil];
      [mapping mapKeyPath:@"id" toAttribute:@"identifier"];
      [mapping mapKeyPath:@"job_id" toAttribute:@"jobID"];
  
      [mapping mapRelationship:@"job" withMapping:[Job objectMapping]];
  
      [mapping connectRelationship:@"job" withObjectForPrimaryKeyAttribute:@"jobID"];
  </code></pre>
  
  <p><strong>I was wondering if there's a way I can do this without the extra jobID property?</strong>  I don't want to have a jobID attribute in the CoreData xcdatamodeld - it's redundant, as the relationship covers that.</p>
  
  <p>Also if I rebuild the NSManagedObjects, I need to re-add the jobID property, which is tedious.  Can't I tell restkit to connect the Room to its corresponding Job via the job_id keypath in the JSON?</p>
  
  <p>If I remove the property, the <code>mapKeyPath:@"job_id"</code> line, and change the last line to <code>[mapping connectRelationship:@"job" withObjectForPrimaryKeyAttribute:@"job_id"];</code> I get</p>
  
  <pre><code>the entity Room is not key value coding-compliant for the key "job_id".
  </code></pre>