How to customize json output from controller in rails -
i have controller returns list of categories , count of items in each category
category.joins(:item).group([:category_id,:name]).count
i clean json formatting
(which category_id, :name, :count )
{"[10, \"fruit.\"]":2,"[11, \"vegetables.\"]":1,"[2, \"pasty.\"]":1}
to this
{"categoryitemcount":[ {"id":10,"name":"fruit","count":7}]}
so custom root name along named columns
thanks in advance!
the solution demoed in railscast #219. create model not backed database , represents data want send back. in case categoryitemcount.rb (id, name, count). built array of these on fly using query given above, , output json.
Comments
Post a Comment