Sameerが言ったことを拡張するには、childの代わりにchild_attributesを送信する必要があります。これは、サーバーからプルしてからサーバーにプッシュするために同じマッピングを使用できないことを意味します。
RestKitでは、元のオブジェクトマッピングとは異なるカスタムシリアル化を指定できます。これは、Railsアプリに投稿する例です。ここで、Object has_many Images
//Map Images
RKManagedObjectMapping* imageMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Image"];
imageMapping.setNilForMissingRelationships = YES;
imageMapping.primaryKeyAttribute = @"imageId";
[imageMapping mapKeyPathsToAttributes:@"id", @"imageId", @"is_thumbnail", @"isThumbnail", @"image_caption", @"imageCaption", @"image_data", @"imageData", nil];
//Serialize Images
RKManagedObjectMapping* imageSerialization = (RKManagedObjectMapping*)[imageMapping inverseMapping];
imageSerialization.rootKeyPath = @"image";
[imageSerialization removeMappingForKeyPath:@"imageId"];
//Map Objects
RKManagedObjectMapping* objectMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Object"];
objectMapping.setNilForMissingRelationships = YES;
objectMapping.primaryKeyAttribute = @"objectId";
[objectMapping mapKeyPath:@"id" toAttribute:@"objectId"];
[objectMapping mapRelationship:@"images" withMapping:imageMapping];
//Serialize Objects
RKManagedObjectMapping* objectSerialization = (RKManagedObjectMapping*)[objectMapping inverseMapping];
objectSerialization.rootKeyPath = @"object";
[objectSerialization removeMappingForKeyPath:@"images"];
[objectSerialization removeMappingForKeyPath:@"objectId"];
[objectSerialization mapKeyPath:@"images" toRelationship:@"images_attributes" withMapping:imageSerialization];
[objectManager.mappingProvider setMapping:objectMapping forKeyPath:@"object"];
[objectManager.mappingProvider setSerializationMapping:objectSerialization forClass:[Object class]];
投稿するときにID属性を削除することも重要であることに注意してください。投稿では問題にならないように思われるので、問題は解決しませんでしたが、レールが外れてしまいます。
価値のあることとして、ネストされたオブジェクトをレールで解析する際にも問題が発生し、コントローラーを次のように変更する必要がありました。
def create
images = params[:object].delete("images_attributes");
@object = Object.new(params[:object])
result = @object.save
if images
images.each do |image|
image.delete(:id)
@object.images.create(image)
end
end
respond_to do |format|
if result
format.html { redirect_to(@object, :notice => 'Object was successfully created.') }
format.json { render :json => @object, :status => :created, :location => @object }
else
format.html { render :action => "new" }
format.json { render :json => @object.errors, :status => :unprocessable_entity }
end
end
end
これは、オブジェクトモデルのbefore_createフィルターに移動できます(おそらく移動する必要があります)。
それが何らかの形で役立つことを願っています。