title: render custom json array in rails
body: <p>Here I have array of Hours in @hours now with relation of user and hour_type.</p>
  
  <p>Hours_controller.rb</p>
  
  <pre><code>def index
    @houra = Hour.all
    @users = User.where(id: @hours.map(&amp;:user_id).uniq)
    @hour_type = HourType.where(id: @hours.map(&amp;:hour_type_id).uniq)
    render json: {
      users: [@users],
      hour_type: [@hour_type],
      hours: @hours
    }
  end
  </code></pre>
  
  <p>this above code is give me response like below.</p>
  
  <pre><code>{
    "users": [
      "id": 6
    ],
    "hour_type": [
      "id": 69
    ],
    "hours": [
      {
        "id": 1,
      },
      {
        "id": 12
      },
      {
        "id": 2,
      }
    ]
  }
  </code></pre>
  
  <p>But Now I want a response in json like below.</p>
  
  <pre><code>{
    "users":[
      0:{
        user_id:2
      },
      1:{
        user_id:3
      },
    ],
  "hours":[
  0:[
    {
      hour_type_id:"1"
      hours: [
        {
          hour_id: 1
        },
  
          hour_id: 2
        },
      ]
    },
  1:[
    {
      hour_type_id:"1"
      hours: [
        {
          hour_id: 1
        },
        {
          hour_id: 2
        },
      ]
    }
  }
  </code></pre>
  
  <p>What can I do to get same response in json?</p>
  
  <p>Here I am adding some what near to result i got </p>
  
  <pre><code>  @hours = Hour.all
    @users = User.where(id: @hours.map(&amp;:user_id).uniq)
    @hour_types = HourType.where(id: @hours.map(&amp;:hour_type_id).uniq)
  
    result = {
      users: @users,
      hours: []
    }
  
    @hour_types.each do |hour_type|
      hour_type_hash = {
        hour_type: hour_type,
        hours: Hour.where(hour_type_id: hour_type.id).all
      }
      result[:hours] &lt;&lt; hour_type_hash
    end
  </code></pre>
  
  <p>render json: result</p>
  
  <p>Now just want to add id before user</p>