如何在渲染 json 中获取 unique/distinct 值

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

我希望在呈现 profesionales.
instituciones 的值是 unique/distinct 模型如图:

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

profesionales_controller.rb去:

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

但我重复了一些 机构。有没有办法应用 uniqdistinct?

在这种情况下,我认为您可能真的想要通过 Institucion 端点提供的 JSON。

如果没有,那么您可能会寻找一种更灵活的方法来生成 JSON,您可以在其中调用 profesional.instrituciones.uniq.each 之类的东西。 jbuilder and rabl 是更灵活的 JSON 模板的好选择。