title: Backbone and Rails associations: Avoiding JSON HashWithIndifferentAccess errors
body: <p>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: </p>
  
  <blockquote>
    <p>Started PUT "/posts/2" for 127.0.0.1 at 2012-01-04 02:36:14 +1000<br>
    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</p>
    
    <p>ActiveRecord::AssociationTypeMismatch (Comment(#70104367824560)
    expected, got
    ActiveSupport::HashWithIndifferentAccess(#70104367278120)):<br>
    app/controllers/posts_controller.rb:62:in <code>block in update'<br>
    app/controllers/posts_controller.rb:61:in</code>update'</p>
  </blockquote>
  
  <p>A few things:</p>
  
  <p>This is triggered on (for example):</p>
  
  <pre><code>c = window.router.comments.models[0]
  c.save({content: 'Changed content'})
  </code></pre>
  
  <p>Also, yes, 'accepts_nested_attributes_for' is present in the model.</p>
  
  <p>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</p>
  
  <h2>RAILS 'POST' MODEL</h2>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    has_many :comments
  
    accepts_nested_attributes_for :comments
  
    def as_json(options = nil)
      super((options || {}).merge(include: { comments: { only: [content] } } ))
    end
  end
  </code></pre>
  
  <h2>RAILS 'COMMENT' MODEL</h2>
  
  <pre><code>class Comment &lt; 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
  </code></pre>
  
  <h2>BACKBONE POST CONTROLLER</h2>
  
  <pre><code>class Backbonerelationaldemo.Models.Post extends Backbone.Model
    paramRoot: 'post'
  
    initialize: () -&gt;
      comments = new Backbonerelationaldemo.Collections.CommentsCollection
      comments.reset(@get('comments'))
      @setComments(comments)
  
    setComments: (comments) -&gt;
      @comments = comments
  
  
  class Backbonerelationaldemo.Collections.PostsCollection extends Backbone.Collection
    model: Backbonerelationaldemo.Models.Post
    url: '/posts'
  </code></pre>
  
  <h2>BACKBONE COMMENTS CONTROLLER</h2>
  
  <pre><code>class Backbonerelationaldemo.Models.Comment extends Backbone.Model
    paramRoot: 'comment'
  
    initialize: () -&gt;
      if (@has('post')) 
        @setPost(new Backbonerelationaldemo.Models.Post(@get('post')))
  
    setPost: (post) -&gt;
      @post = post
  
  class Backbonerelationaldemo.Collections.CommentsCollection extends Backbone.Collection
    model: Backbonerelationaldemo.Models.Comment
    url: '/comments'
  </code></pre>