jeudi 24 novembre 2016

Ruby hash returning strange values

I am returning a response of user fields in JSON. I am creating JSON as below.

def user_response(users)
    users_array = []
    users.each do |user|
      uhash = {}
      uhash[:id] = user.id,
      uhash[:nickname] = user.nickname,
      uhash[:online_sharing] = user.online_sharing,
      uhash[:offline_export] = user.offline_export,
      uhash[:created_at] = user.created_at,
      uhash[:app_opens_count] = user.app_opens_count,
      uhash[:last_activity] = user.last_activity,
      uhash[:activity_goal] = user.activity_goal,
      uhash[:last_activity] = user.last_activity,
      uhash[:region] = user.region
      users_array << uhash
    end
    users_array
  end

But the response is pretty weird. The :id key in hash has an array of all the fields don't know why.

{
    "nickname": "adidas",
    "online_sharing": null,
    "offline_export": null,
    "created_at": "2016-08-26T09:03:54.000Z",
    "app_opens_count": 29,
    "last_activity": "2016-08-26T09:13:01.000Z",
    "activity_goal": 3,
    "region": "US",
    "id": [
      9635,
      "adidas",
      null,
      null,
      "2016-08-26T09:03:54.000Z",
      29,
      "2016-08-26T09:13:01.000Z",
      3,
      "2016-08-26T09:13:01.000Z",
      "US"
    ]
  }

Aucun commentaire:

Enregistrer un commentaire