我有一个渲染json的控制器。这是代码:
class AppLaunchDataController < ApiController
def index
service_types = []
vendors = []
tariffs = []
fields = []
vendors_hash = {}
service_types_hash = {}
tariffs_hash = {}
fields_hash = {}
@service_types = ServiceType.select("title, id").all.each do |service_type|
service_types_hash = {id: service_type.id, title: service_type.title}
service_types << service_types_hash
@vendors = service_type.vendors.select("title, id").all.each do |vendor|
vendors_hash = {id: vendor.id, title: vendor.title}
vendors << vendors_hash
@tariff = vendor.tariffs.select("title, id").all.each do |tariff|
tariffs_hash = {id: tariff.id, title: tariff.title}
tariffs << tariffs_hash
@fields = tariff.fields.select("id, current_value, value_list").all.each do |field|
fields_hash = {id: field.id, current_value: field.current_value, value_list: field.value_list}
fields << fields_hash
end
tariffs_hash[:fields] = fields
fields = []
end
vendors_hash[:tariffs] = tariffs
tariffs = []
end
service_types_hash[:vendors] = vendors
vendors = []
end
render json: service_types
end
end
返回值如下所示:
[{"id":1,"title":"Water",
"vendors":[{"id":1,"title":"Vendor_1",
"tariffs":[{"id":1,"title":"Unlim",
"fields":[{"id":1,"current_value":"200","value_list":null},{"id":2,"current_value":"Value_1","value_list":"Value_1, Value_2, Value_3"}]},{"id":2,"title":"Volume",
"fields":[]}]},
{"id":2,"title":"Vendor_2",
"tariffs":[]}]},
{"id":2,"title":"Gas",
"vendors":[]},
{"id":3,"title":"Internet",
"vendors":[]}]
它有效,但我确信还有另一种(更多的轨道)方式来获得结果。 如果有人以前处理过,请帮助。感谢。
答案 0 :(得分:0)
只需使用
# for eager-loading :
@service_types = ServiceType.includes( vendors: {tariffs: :fields} )
# now for the json :
@service_types.to_json( include: {vendors: {include: {tariffs: { include: :fields}}}} )
如果您的ServiceType
对象始终具有此类表示,则只需覆盖模型的as_json
方法:
class ServiceType
def as_json( options={} )
super( {include: :vendors }.merge(options) ) # vendors, etc.
end
end
鼓励在rails中执行此操作:在模型上调用to_json
只会调用as_json
,可能还有其他选项。实际上,as_json
描述了此模型的规范json表示。有关详情,请参阅to_json
上的api dock。
如果您的需求更加特殊(使用选择以获得更快的查询),您可以随时在模型上滚动自己的to_json_for_app_launch_data
方法(使用或不使用as_json
),甚至更好presenter