Home How to get unique/distinct values in included in render json
Reply: 1

How to get unique/distinct values in included in render json

Bruno Calmels
1#
Bruno Calmels Published in 2017-11-14 20:38:25Z

I'd like the instituciones values to be unique/distinct when rendering profesionales.
The model is as shown:

class Profesional < ApplicationRecord  
has_many :instituciones, through: :horarios

and profesionales_controller.rb goes:

render json: @profesionales.to_json(include: {   
        instituciones:  { only: [:id] }  
}) 

But I get some instituciones repeated. Is there a way to apply uniq or distinct?

Daniel Westendorf
2#
Daniel Westendorf Reply to 2017-11-14 20:49:44Z

In this case, I think you might really want the JSON which would be served via an Institucion endpoint.

If not, then you might look to a more flexible means of generating the JSON where you can call something like profesional.instrituciones.uniq.each. jbuilder and rabl are good options for more flexible JSON templates.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312126 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO