Aniket Tiwari
Aniket Tiwari

Reputation: 3998

Generate array of hashes

I am getting value in the @user object

[{"id":1,"name":"wjsisi"," code":"shsh","address":"shsb","landmark":"Bah"," area":4949.0," volume":4949," value":49499,"created_at":"2016-06-07T05:46:53.000Z","updated_at":"2016-06-07T05:46:53.000Z","number":"35853","state":"Assam","city":"Diphu","father":null," phone":null,"user_id":8},

{"id":2,"name":"wjsisi","code":"shsh","address":"sdsd","landmark":"Bah","area":4949.0,"volume":4949,"sales_value":49499,"created_at":"2016-06-07T05:49:23.000Z","updated_at":"2016-06-07T05:49:23.000Z","number":"1234","state":"Assam","city":"dddd","father":null," phone":null,"user_id":8}]


Here is my code

 render :json => {:result => true,:object =>   @user.map{|x|  [ x[:id],x[:code],x[:name] ] }   }     


This is my output

{
  "result": true,
  "object": [
    [
      1,
      "shsh",
      "wjsisi"
    ],
    [
      2,
      "shsh",
      "wjsisi"
    ],
    [
      4,
      "shsh",
      "wjsisi"
    ]
  ]
}


I want to render in array of hashes with key and value

  [
    {id: '',name: '',code: ''},
    {id: '',name: '',code: ''},
    ]

Upvotes: 0

Views: 59

Answers (1)

Harry Bomrah
Harry Bomrah

Reputation: 1668

You should do like this,

render :json => {:result => true,:object => @user.map{|x|  {"id" => x[:id],"code" => x[:code],"name" => x[:name]}}}

I hope this is what you need.

Upvotes: 3

Related Questions