Как представить свойства объекта в заданном контексте?

Ищу решение своей проблемы У меня есть отношение => has_many Councils, через .

И я хотел бы отобразить Компанию в контексте данного Совета, так что если CouncilCompany имеет свойство присутствует отображать его по умолчанию Company name.

      # == Schema Information
#
# Table name: council_companies
#
#  id            :uuid             not null, primary key
#  name          :string
# == Schema Information
#
# Table name: companies
#
#  id                   :uuid             not null, primary key
#  name                 :string          default(FALSE), not null
      render json: Company::Representer::Show.new(@company).to_json(
  current_user: current_api_v1_user,
  council: @council
)
      require 'representable/json'
module Company::Representer
  class Show < Representable::Decorator
    include Representable::JSON

    Company.columns_hash.keys.each do |column|
      property column.to_sym.as_json, render_nil: true
    end
  end
end

Как лучше всего это сделать? Уже пытался найти решение здесь: https://trailblazer.to/2.1/docs/presentable.html#presentable-api

1 ответ

Как насчет определения представителя для CouncilCompany вместо этого, поскольку он принадлежит Company ?

      require 'representable/json'

module CouncilCompany::Representer
  class Show < Representable::Decorator
    include Representable::JSON

    property :name, default: -> { company.name }

    property :company do
      property :id
      property :name
      ...
    end
  end
end
      render json: CouncilCompany::Representer::Show.new(@council_company).to_json(
  current_user: current_api_v1_user
)
Другие вопросы по тегам