3

I need some help I have a controller with an action that queries two models. Now I need to send both of them as json in order to be used on my angular views.

In the example bellow how should I send the "complex" and its "fields" in one json response?

Ex.

def complexes_and_fields
  complex = Complex.find(params[:id])
  search_params = {complex_id: complex._id}
  fields = Field.where(search_params)
  if !complex.nil?
    render json: ???.to_json, status: :ok
  else
    render json: { error_description: 'no complex found' },status: :bad_request
end
Pablo Palacios
  • 2,767
  • 20
  • 37

2 Answers2

4

An easy way to do this is to build a hash with your objects

complex = Complex.find(params[:id])
search_params = {complex_id: complex._id}
fields = Field.where(search_params)

render json: { complex: complex, fields: fields, search_params: search_params }, status: :ok

Another way would be to user a view such as some_view.json.erb where you render the objects as you are expecting it in your angular view. Also you can use can use ActiveModelSerializers, read on https://github.com/rails-api/active_model_serializers

Ideally what you will want to do is encapsulate this response into its object and make a single call in your controller that returns you the results

Without going into too much details something like this

results = MyComplexFieldsObj.response(params[:id])
render son: results, status: :ok
Elias Perez
  • 413
  • 3
  • 8
  • Thank you, the first option helped me. Ended understanding and implementing your solution with Jim post help. Thanks. – Pablo Palacios Aug 05 '16 at 21:40
  • Glad you found it helpful @Pablo. Im also like the fact you went with the last solution ;) I'm not a fan of recurring to a gem when you can arrive to a solution without dependencies, unless is something complicated. – Elias Perez Aug 05 '16 at 21:50
  • I love simplicity as well, in my experience the failure mode with early binding to JSON is quite bad. When you are listing blog posts, let's say, you include the bodies, but then in many cases you only want the titles and authors. You end up passing in options to to_json or the like, and soon enough you have so many cases that you wish you had just built objects. It's fine to get started, but be careful! And when you need to version your APIs, nothing pure model based holds up. – Jim Van Fleet Aug 09 '16 at 17:50
1

This is an extremely common requirement in Rails applications. This need is rarely restricted to a single model, or a single location. As a result, a variety of gems exist to provide this kind of functionality (in many cases, without altering the signature of your render lines substantially).

This post offers a good listing. Personally, I've had a good experience with active_model_serializers and an acceptable experience with grape-entity. It's reasonable to review their documentation and decide which is best for you.

Jim Van Fleet
  • 1,110
  • 7
  • 12
  • Thank you the page gave me a lot of understanding of how to solve my need in may ways. Ended using the last solution. Thanks – Pablo Palacios Aug 05 '16 at 21:38