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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:52:24+00:00 2026-05-25T05:52:24+00:00

I am getting some weird behavior when creating models with backbone.js and rails 3.0.7

  • 0

I am getting some weird behavior when creating models with backbone.js and rails 3.0.7 (and coffeescript).

I create a model with

@model = @collection.create
  param : a_param
  param : another_param

This saves to rails fine and the return object is what I expect (when I console.log @model it returns a proper looking backbone model with id, cid, attributes…etc).

When I console.log @model.id it returns undefined (Even though it was there in console.log @model).

When I console.log @collection it returns the expected collection, but when I console.log @collection.models it returns an empty array.

When I load the @collection via json in the (rails)page:

( json_from_the_page : #{@collection.to_json} )   

@collection = new CollectionName()
@collection.reset options.json_from_the_page

and console.log @collection.models everything works fine so it must be something to do with saving to the server?

Any ideas, or places I should look in the backbone code to further debug?

  • 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-25T05:52:25+00:00Added an answer on May 25, 2026 at 5:52 am

    According to the source code for Backbone, the ID of an object is only set if it receives the ID during construction. Rails is setting the ID, so when you ask for it from rails, you get one, but you don’t get one when you build a new object.

    This is by design; backbone assumes that the canonical ID will be set by the server. So for a “freshly made” model, the ID isn’t set automatically (and the model’s method isNew() returns true).

    If you want the freshly made model to have an ID, save it immediately (and don’t act on it until the save returns) so your server can provide an ID. If you have a programmatic ID that is reliable on the client side, override initialize() to provide one during construction if one is not provided in the attributes field.

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

Sidebar

Related Questions

I'm getting some weird behavior in asp.net MVC2 (at least it's not what I'm
I'm getting some weird behavior from IE when trying to change a session variable
I'm writing an IME (soft keyboard) and I'm getting some weird behavior from one
I am getting some weird behavior when using cout in my programm, which is
I'm getting some weird behavior with a vector in C++ I was hoping someone
I'm using Fluid 960 at the moment, and I'm getting some weird behavior with
I'm getting some weird behaviour recompiling some applications in 2009 that used widestrings at
I'm getting some weird black spots with a custom map page (via the Google
I'm working on a school project and I'm getting some weird errors from Xcode.
We're getting some funny behavior in the Visual Studio debugger with the following. I'm

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.