I’m generating JSON as follows:
def suggest_gems
@allgems = RubyGem.where("name like ?", "%#{params[:q]}%")
respond_to do |format|
format.json { render :json => @allgems.to_json }
end
end
However, the RubyGem table is quite large, with values for descriptions, links and created, updated at. How can I only output the name and id in the generated JSON to decrease loading times?
Thanks.
So I figured how to do that without changing the
as_jsonmethod or using therespond_withstatement, by adding a few lines of code. Here’s what I did:I basically defined an array and associated the
nameandidattributes accordingly, then formatted the array as JSON. Shaved around 6 seconds off the loading time 🙂Adding this answer here so that others might find it helpful!