C2FO / patio

Idiomatic database toolkit

Home Page:http://c2fo.github.io/patio

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Modify toHash to always return on object of arrays and support duplicate index columns

vikeen opened this issue · comments

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" }
  ]
}