1

私はこのようなクラス構造を持っています:

class Parent < ActiveRecord::Base
    has_one :child
    accepts_nested_attributes_for :child
end

...

class Child < ActiveRecord::Base
    belongs_to :parent
    validates :text, :presence => true
end

私のモバイルアプリ(IOS + RestKit)でHTTP POST、新しいParentオブジェクトを作成するためのリクエストを送信します。このリクエストにはContent-Typeがありますapplication/json

リクエストは、次のようになります。

{"parent":{"field1":"value1","child":{"text":"textvalue"},"duration":100}}

Railsがそれを受け取り、保存しようとすると、次のエラーが発生します。

ActiveRecord::AssociationTypeMismatch (Child(#2198796760) expected, got ActiveSupport::HashWithIndifferentAccess(#2158000780)):

誰が何が起こっているのか知っていますか?

4

2 に答える 2

3

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フィルターに移動できます(おそらく移動する必要があります)。

それが何らかの形で役立つことを願っています。

于 2011-08-05T06:12:26.633 に答える
2

paramsハッシュの「child」の代わりに「child_attributes」を使用してください。

于 2011-08-05T02:44:12.377 に答える