Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6235999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:46:28+00:00 2026-05-24T10:46:28+00:00

I have a class structure like this: class Parent < ActiveRecord::Base has_one :child accepts_nested_attributes_for

  • 0

I have a class structure like this:

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

In my mobile app (IOS + RestKit), I submit a HTTP POST request to create a new Parent object. This request has Content-Type application/json

The request looks like this on the wire:

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

When Rails receives it, and tries to save it, I get the error:

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

Anyone know what’s going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:46:29+00:00Added an answer on May 24, 2026 at 10:46 am

    To expand one what Sameer said, you need to send child_attributes instead of child. This means you can’t use the same mapping for pulling from the server and then pushing to it.

    In RestKit you can specify a custom serialization that’s different from the original object mapping. Here’s an example that posts to a rails app, where 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]];
    

    Note that it’s also important to remove the ID attributes when posting – that gave me no end of trouble, since it doesn’t seem like it should matter in a post, but it throws rails off.

    For what it’s worth, I also had issues parsing the nested objects with rails, and had to change my controller to look like this:

    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
    

    That could (and probably should) be moved into a before_create filter on the Object model.

    I hope that helps in some way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.