Modify toHash to always return on object of arrays and support duplicate index columns
vikeen opened this issue · comments
John Rake commented
Currently toHash()
assumes the column being indexed to be unique.
myModel.toHash("some_unique_key");
However, if the column being indexed is not actually unique you will have an undesired effect of the method overriding your data output
Take this data set for example.
name | gender |
---|---|
Bob | M |
Stan | M |
Rachel | F |
Julia | F |
Fred | M |
Now when you run your data request
person.toHash("gender");
You will get back an object looking something like the following
{
"M": { "name": "Fred", "gender": "M" },
"F": { "name": "Julia", "gender": "F" }
}
This happens because the toHash
method is overriding the index key in the return type after each iteration. I would propose that we make this consistent and instead return a grouped data set result.
{
"M": [
{ "name": "Bob", "gender": "M" }
{ "name": "Stan", "gender": "M" }
{ "name": "Fred", "gender": "M" }
],
"F": [
{ "name": "Rachel", "gender": "F" }
{ "name": "Julia", "gender": "F" }
]
}