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 126425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:18:57+00:00 2026-05-11T05:18:57+00:00

My aim is to create nested resources via one REST request. The REST requests

  • 0

My aim is to create nested resources via one REST request. The REST requests is represented via a XML document. That works fine for single resources but I could not manage it for nested ones. OK I’ll give you a little example next.

First create a new rails project

rails forrest 

Next we generate the scaffolds of two resources, the trees and the bird’s nests.

./script/generate scaffold tree name:string ./script/generate scaffold bird_nest tree_id:integer bird_type:string eggs_count:integer 

In the File ./forrest/app/models/tree.rb we insert the ‘has_many’ line below because a tree can have many bird’s nests 🙂

class Tree < ActiveRecord::Base   has_many :bird_nests end 

In the File ./forrest/app/models/bird_nest.rb we insert the ‘belongs_to’ line below because every bird’s nest should belong to a tree.

class BirdNest < ActiveRecord::Base   belongs_to :tree end 

Afterwards we set up the database and start the server:

rake db:create rake db:migrate ./script/server 

Just copy and paste this XML sniplet to a file named ‘tree.xml’…

<tree>   <name>Apple</name> </tree> 

…and post it to the service by cURL to create a new tree:

curl  -H 'Content-type: application/xml' -H 'Accept: application/xml' -d @tree.xml http://localhost:3000/trees/ -X POST 

This works fine. Also for the bird’s nest XML (file name ‘bird-nest.xml’) separately. If we send this…

<bird-nest>   <tree-id>1</tree-id>   <bird-type>Sparrow</bird-type>   <eggs-count>2</eggs-count> </bird-nest> 

…also via the following cURL statement. That resource is created properly!

curl  -H 'Content-type: application/xml' -H 'Accept: application/xml' -d @bird-nest.xml http://localhost:3000/bird_nests/ -X POST 

OK everything is fine so far. Now comes the point where the rubber meets the road. We create both resources in one request. So here is the XML for our tree which contains one bird’s nest:

<tree>   <name>Cherry</name>   <bird-nests>     <bird-nest>       <bird-type>Blackbird</bird-type>       <eggs-count>2</eggs-count>     </bird-nest>   </bird-nests> </tree> 

We trigger the appropriate request by using cURL again…

curl  -H 'Content-type: application/xml' -H 'Accept: application/xml' -d @tree-and-bird_nest.xml http://localhost:3000/trees/ -X POST 

…and now we’ll get a server error in the (generated) ‘create’ method of the tree’s controller: AssociationTypeMismatch (BirdNest expected, got Array)

In my point of view this is the important part of the server’s log regarding received attributes and error message:

Processing TreesController#create (for 127.0.0.1 at 2009-02-17 11:29:20) [POST]   Session ID: 8373b8df7629332d4e251a18e844c7f9   Parameters: {'action'=>'create', 'controller'=>'trees', 'tree'=>{'name'=>'Cherry', 'bird_nests'=>{'bird_nest'=>{'bird_type'=>'Blackbird', 'eggs_count'=>'2'}}}}   SQL (0.000082)   SET NAMES 'utf8'   SQL (0.000051)   SET SQL_AUTO_IS_NULL=0   Tree Columns (0.000544)   SHOW FIELDS FROM `trees`       ActiveRecord::AssociationTypeMismatch (BirdNest expected, got Array):         /vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:150:in `raise_on_type_mismatch'         /vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:146:in `replace'         /vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:146:in `each'         /vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:146:in `replace'         /vendor/rails/activerecord/lib/active_record/associations.rb:1048:in `bird_nests='         /vendor/rails/activerecord/lib/active_record/base.rb:2117:in `send'         /vendor/rails/activerecord/lib/active_record/base.rb:2117:in `attributes='         /vendor/rails/activerecord/lib/active_record/base.rb:2116:in `each'         /vendor/rails/activerecord/lib/active_record/base.rb:2116:in `attributes='         /vendor/rails/activerecord/lib/active_record/base.rb:1926:in `initialize'         /app/controllers/trees_controller.rb:43:in `new'         /app/controllers/trees_controller.rb:43:in `create' 

So my question is what I’m doing wrong regarding the nesting of the XML resources. Which would be the right XML syntax? Or do I have to modify the tree’s controller manually as this case is not covered by the generated one?

  • 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. 2026-05-11T05:18:57+00:00Added an answer on May 11, 2026 at 5:18 am

    One way you can accomplish this is to override the bird_nests= method on your tree model.

    def bird_nests=(attrs_array)   attrs_array.each do |attrs|     bird_nests.build(attrs)   end end 

    The only issue here is that you lose the default behavior of the setter, which may or may not be an issue in your app.

    If you’re running a more recent version of Rails you can just turn on mass assignment as described here:

    http://github.com/rails/rails/commit/e0750d6a5c7f621e4ca12205137c0b135cab444a

    And here:

    http://ryandaigle.com/articles/2008/7/19/what-s-new-in-edge-rails-nested-models

    class Tree < ActiveRecord::Base   has_many :bird_nests, :accessible => true end 

    This is the preferred option.

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer "Detach All" will leave the process running "Stop Debugging" will… May 11, 2026 at 11:38 pm
  • Editorial Team
    Editorial Team added an answer There's a utility called One-Jar which does what you want,… May 11, 2026 at 11:38 pm
  • Editorial Team
    Editorial Team added an answer In the Page_PreInit method where you assign themes, there's a… May 11, 2026 at 11:38 pm

Related Questions

Is it possible to set a symbol for conditional compilation by setting up properties
How can I emulate classes (and namespaces) in JavaScript? I need to create a
With Adobe Flex I would create a custom ItemRenderer to change the display of
I have a PHP script which changes file permissions on my server using chmod.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.