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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:54:17+00:00 2026-06-11T18:54:17+00:00

I’m writing a practice Backbone app, with Rails backend API, and I’m confused about

  • 0

I’m writing a practice Backbone app, with Rails backend API, and I’m confused about the behavior of save on Backbone models.

Let’s say a Team has many Players, and I want to save a team with numerous players in a single POST.

So in Rails I have:

class Team < ActiveRecord::Base
  has_many :players
  accepts_nested_attributes_for :players
end

class Player < ActiveRecod::Base
  belongs_to :team
end

and for backbone client, I have a Player model and a Players collection defined (not shown)

and then the containing Team model (NOTE: no Teams collection)

Demo.Models.Team = Backbone.Model.extend({
  urlRoot: '/teams',
  defaults: {
    'team_size': 12
  },
  initialize: function() {
    this.players = new Demo.Collections.Players());
  },
  toJSON: function() {
    var json = _.clone(this.attributes);
    json.players_attributes = this.players.map(function(player) {
      return player.toJSON();
    });
    return json;  
  }
}

When I examine my stringified JSON in the browser, everything looks good:

{"team_size":12, "players_attributes":[{"name":"Fred"},{"name":"Jim" },{"name":"Mark"}]}

Checking the server logs, the lone top level attribute (‘team size’) is repeated, once at the top level, and then repeated under a root key.

Started POST "/teams" for 127.0.0.1 at 2012-06-07 13:39:40 -0400
Processing by TeamsController#create as JSON
  Parameters: {
    "team_size"=>12, "players_attributes":[{"name":"Fred"},{"name":"Jim" },{"name":"Mark"}]}, 
    "team"=>{"team_size"=>12}
  }

I have a few questions:

  1. What’s the best way to ensure the player_attributes are nested inside the root key? I (So that I can do a nested save inside TeamController, in the standard rails manner: (i.e. Team.create(params[:team]) ) I can accomplish this with some javascript hackery inside toJSON, but I’m guessing there’s an easier, cleaner way.

  2. Is this standard, desirable behaviour? To send duplicates of attributes like this? I guess there’s no harm, but it doesn’t smell right.

  3. Am I not defining the url / urlRoot correctly or some such?

thanks

  • 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-06-11T18:54:18+00:00Added an answer on June 11, 2026 at 6:54 pm

    1- You have to override the toJSON method in order to include the model name as the root of the JSON element sent to the server.

     toJSON: function() {
        return { team: _.clone( this.attributes ) }
      },
    

    Since you are already messing and overriding this method I don’t see any reasons not to go this way.

    2- This is a very strange behavior you’re describing. Try:

    class Team < ActiveRecord::Base
      self.include_root_in_json = false
    end
    

    It will probably eliminate Rails duplicate params parsing. Another advantage you get from this is that Rails won’t include the team as a root element of its generated JSON to the client.

    3- Your definition of urlRoot is just fine.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.