0

how would I go about adding meta data to a json response in rails 3.2? Is there some way to access the model metadata and inclue that? ie. the field name, and the data type.

Something like the following:

{
"metaData":{
  "columns":[
     {
        "dataIndex":"id",
        "text":"User ID",
        "type":"integer"
     },
     {
        "dataIndex":"name",
        "text":"User Name",
        "type":"string"
     },
     {
        "dataIndex":"birthday",
        "format":"dd-mmm-yy",
        "text":"Birthday",
        "type":"datetime"
     }
  ]
},
"data":[
  {
     "id":1,
     "name":"Queen Elizabeth",
     "birthday":"1533-09-07T06:33:39Z"
  },
  {
     "id":2,
     "name":"Queen Elizabeth II",
     "birthday":"1926-04-21T02:40:00Z"
  }
]
}

2 Answers 2

1

Your model class will have a columns method that returns an array of column objects. You could extract most of your metadata from that:

metas = Model.columns.map { |c| c.as_json.slice('name', 'type') }

then you can add that to your response.

Sign up to request clarification or add additional context in comments.

1 Comment

.columns is exactly what i was looking for thanks. so obvious too. Thanks!
0

I would probably build that up with something like jbuilder

There's a good railscast episode on how to use it http://railscasts.com/episodes/320-jbuilder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.