如何在给定上下文中表示对象属性?
How to represent object properties in given context?
我正在寻找我的问题的解决方案 我有关系 => Company
has_many Councils
,通过 CouncilCompany
.
而且我想在给定委员会的上下文中显示公司,所以如果 CouncilCompany
有 name
属性 显示默认值 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/representable.html#representable-api
如何为 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
)
我正在寻找我的问题的解决方案 我有关系 => Company
has_many Councils
,通过 CouncilCompany
.
而且我想在给定委员会的上下文中显示公司,所以如果 CouncilCompany
有 name
属性 显示默认值 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/representable.html#representable-api
如何为 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
)