batmanjs/batman — pull request #1065 by Arjeno (2014-06-23 12:40:30+00:00)
Title: HasMany toJSON with index

When saving an `hasMany` with `saveInline: true` it maps the JSON attributes as arrays. Which then is 'flattened' by a serializer. The server receives something like this:

```
product[name]:iPhone
product[variations][][id]:1
product[variations][][values][]:5c
product[variations][][values][]:White
product[variations][][id]:2
product[variations][][values][]:5s
product[variations][][values][]:Spacegray
```

However this doesn't work for new records being inserted. This is what happens:

```
product[name]:iPhone
product[variations][][values][]:5c
product[variations][][values][]:White
product[variations][][values][]:5s
product[variations][][values][]:Spacegray
```

Which then is translated to:

```ruby
{
:product => {
:name => 'iPhone',
:variations => [
{
:values => ['5c', 'White', '5s', 'Spacegray']
}
]
}
}
```

This pull request adds index to the toJSON() for a `hasMany`. This makes sure the attributes are properly sent to the server:

```ruby
{
:product => {
:name => 'iPhone',
:variations => {
0 => {
:values => ['5c', 'White']
},
1 => {
:values => ['5s', 'Spacegray']
}
}
}
}
```
I guess this may be an edge-case which I ran into, but serializing data into this makes sense to me. What do you think?