1

I've seen the answer regarding changing the toJSON function in backbone to create a nested model Saving nested objects with Rails, backbone.js, and accepts_nested_attributes_for,

But I'm trying to understand why this works, and why it is any different than just creating

var book = new Book({title: 'my first book', chapters:[{chapter_title: 'first chapter'},{chapter_title: 'second chapter'}...]});
book.url ='books';
book.save();

I've been trying to save to rails using the manual method, but i can't save the chapters. I find this strange, as far as I can tell the json structure is there, so what does the 'to_JSON' change recommended for nested attributes do? Why is it necessary?

As backbone models don't need to have model attributes defined, will there be problems when a nested models is returned? Or will it just parse the entire json without issue?

Community
  • 1
  • 1
pedalpete
  • 21,076
  • 45
  • 128
  • 239

1 Answers1

0

I am not very familiar with Rails, but it seems to me that the benefit of overloading the toJSON function is that chapters, in this instance, could be a model, or a collection or something, and have the extra functionality of Backbone.js as opposed to just being a JSON object.

Jason
  • 1,114
  • 1
  • 10
  • 24