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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:49:05+00:00 2026-05-27T21:49:05+00:00

I’m trying to get my backbone associations working inside a rails app , and

  • 0

I’m trying to get my backbone associations working inside a rails app , and I’m having difficulty when trying to update existing models. Specifically, Rails throws the following error:

Started PUT “/posts/2” for 127.0.0.1 at 2012-01-04 02:36:14 +1000
Processing by PostsController#update as JSON Parameters:
{“post”=>{“content”=>”Seconderona”,
“created_at”=>”2012-01-03T10:51:09Z”, “id”=>2, “title”=>”Second test
post”, “updated_at”=>”2012-01-03T10:51:09Z”, “comments”=>[{}]},
“id”=>”2”} Post Load (0.2ms) SELECT “posts”.* FROM “posts” WHERE
“posts”.”id” = ? LIMIT 1 [[“id”, “2”]] WARNING: Can’t mass-assign
protected attributes: id Completed 500 Internal Server Error in 15ms

ActiveRecord::AssociationTypeMismatch (Comment(#70104367824560)
expected, got
ActiveSupport::HashWithIndifferentAccess(#70104367278120)):
app/controllers/posts_controller.rb:62:in block in update'
app/controllers/posts_controller.rb:61:in
update’

A few things:

This is triggered on (for example):

c = window.router.comments.models[0]
c.save({content: 'Changed content'})

Also, yes, ‘accepts_nested_attributes_for’ is present in the model.

The (offending) code below is taken pretty much verbatim from thougtbot’s “backbone on rails” ebook, and I’ve also tried following the documentation for the backbone-relational gem. Both raise this error. Any ideas appreciated, code below

RAILS ‘POST’ MODEL

class Post < ActiveRecord::Base
  has_many :comments

  accepts_nested_attributes_for :comments

  def as_json(options = nil)
    super((options || {}).merge(include: { comments: { only: [content] } } ))
  end
end

RAILS ‘COMMENT’ MODEL

class Comment < ActiveRecord::Base
  belongs_to :post

  accepts_nested_attributes_for :post

  def as_json(options = nil)
    super((options || {}).merge(include: { post: { only: [:title, :content]}}))
  end
end

BACKBONE POST CONTROLLER

class Backbonerelationaldemo.Models.Post extends Backbone.Model
  paramRoot: 'post'

  initialize: () ->
    comments = new Backbonerelationaldemo.Collections.CommentsCollection
    comments.reset(@get('comments'))
    @setComments(comments)

  setComments: (comments) ->
    @comments = comments


class Backbonerelationaldemo.Collections.PostsCollection extends Backbone.Collection
  model: Backbonerelationaldemo.Models.Post
  url: '/posts'

BACKBONE COMMENTS CONTROLLER

class Backbonerelationaldemo.Models.Comment extends Backbone.Model
  paramRoot: 'comment'

  initialize: () ->
    if (@has('post')) 
      @setPost(new Backbonerelationaldemo.Models.Post(@get('post')))

  setPost: (post) ->
    @post = post

class Backbonerelationaldemo.Collections.CommentsCollection extends Backbone.Collection
  model: Backbonerelationaldemo.Models.Comment
  url: '/comments'
  • 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-27T21:49:06+00:00Added an answer on May 27, 2026 at 9:49 pm

    I dealt with the same issue recently. It’s actually not a HashWithIndifferentAccess error: it has to do with how accepts_nested_attributes_for expects params.

    When you declare accepts_nested_attributes_for :comments, Rails looks for a parameter call comments_attributes on the incoming params.

    The problem is that your JSON representation coming from Backbone has a "comments" property instead of a "comments_attributes" property.

    You could fix it on the Backbone side by adding a toJSON function to your Post model:

    # in your Post model
    toJSON: ->
      attrs = _.clone(@attributes)
      attrs.comments_attributes = _.clone(@attributes.comments)
      delete attrs.comments
      attrs
    

    Or you could handle it in your Rails controller:

    # in your Posts controller
    def update
      params[:comments_attributes] = params.delete(:comments) if params.has_key? :comments
      # call to update_attributes and whatever else you need to do
     end
    

    Hopefully this helps.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am writing an app with both english and french support. The app requests
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.