How can I return json data from multiple rails Models? -
i have created backend rails server strictly serves ios app built. upon initial loading of ios app, needs pull in dozen models , data. want avoid 1) dozen separate server calls , b) chaining dozen calls in completions blocks. i.e. call a, when done, call b, when b done, call c... etc.
i make load resource. return data dozen models in 1 call. resulting json like...
{ "widgets": [ { "id": 1, "desc": "one" }, { "id": 2, "desc": "two" } ], "gadgets": [ { "id": 1, "desc": "one" } ], "flidgets": [ { "id": 1, "desc": "one" } ] }
i prefer not include timestamps.
how can this? suppose create new controller, initialloadcontroller. model data of dozen objects. how can render dozen models json , format this?
please check code below:
class initialloadscontroller < applicationcontroller def load widgets = widget.select(:id, :desc) gadgets = gadget.select(:id, :desc) flidgets = flidget.select(:id, :desc) response = { widgets: widgets, gadgets: gadgets, flidgets: flidgets } render json: response end end
even can use jbuilder render json response (as alternative render json: response).
Comments
Post a Comment