1

我有一个呈现 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":[]}]

它有效,但我确信还有另一种(更多的 rails-)方法可以获得结果。如果有人以前处理过,请帮助。谢谢。

4

1 回答 1

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 表示。有关更多信息,请参阅api dockto_json

If your needs are more peculiar ( as using selects for a faster query ), you can always roll your own to_json_for_app_launch_data method on the model (using or not as_json), or even better on a presenter

于 2013-01-28T13:09:02.400 回答