【问题标题】:Nested attributes have root in rails 4 when y render JSON渲染 JSON 时,嵌套属性在 rails 4 中有根
【发布时间】:2016-05-24 05:59:44
【问题描述】:

我的 Rails 应用程序出现问题。 当我渲染 JSON 时,嵌套属性会在内部重复(如 JSON 示例)。 这是我的 API 控制器、序列化器、模型和 JSON 响应。

PS:经过大量更新后,我开始处理此问题,在此之前,它工作正常。我不知道是什么。我的 ruby​​ 版本是 ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux],我的 rails 版本是 Rails 4.2.4

PS2:对不起我的英语不好

ingreso.rb

class Ingreso < ActiveRecord::Base

  belongs_to :departamento, touch: true
  belongs_to :concepto
[...]
end

departamento.rb

class Departamento < ActiveRecord::Base
  has_many :ingresos
  [...]
end

api/serializers/ingreso_controller.rb

class Api::V1::IngresosController < Api::V1::BaseController
  include ActiveHashRelation

  before_filter :authenticate_user!


  def show_for_departamento
    ingresos = Ingreso.where(departamento_id:params[:id]).order("id DESC")
    render(
      json: ActiveModel::ArraySerializer.new(
        ingresos,
        each_serializer: Api::V1::IngresoSerializer,
        root: 'ingresos'
        )
      )
  end

api/serializers/ingreso_serializer.rb

class Api::V1::IngresoSerializer < Api::V1::BaseSerializer
  attributes :field1, :field2, :field3, :field4, :field5, :concepto, :colaborador, :departamento

  def formapago
    object.try(:formapago).try(:nombre) || "Sin información"
  end

  def colaborador
    object.try(:colaborador).try(:full_name) || "Sin información"
  end
  def created_at
    object.created_at.in_time_zone.iso8601 if object.created_at
  end

  def updated_at
    object.updated_at.in_time_zone.iso8601 if object.created_at
  end

JSON

{
  "myRoot": [
    {
      "field1": 13201,
      "field2": -5720,
      "field3": null,
      "field4": "2016-04-11T00:00:00.000-04:00",
      "field5": "2016-04-10T00:00:00.000-04:00",

      "concepto": {
        "concepto": {
          "data1": 1,
          "data2": "Gasto Común",
          "created_at": "2013-01-03T15:49:16.000-04:00",
          "updated_at": "2015-02-05T15:54:12.363-04:00",
          "data3": 1,
          "data4": "gasto-comun",
          "data5": false
        }
      },
      "colaborador": "Sin información",
      "departamento": {
        "departamento": {
          "another_field1": 101,
          "another_field2": "1305",
          "another_field3": 370,
          "another_field4": 1,
          "created_at": "2013-01-05T01:05:36.000-04:00",
          "updated_at": "2016-05-20T11:48:30.892-04:00",
        }
      }
    }
  ]
}

【问题讨论】:

    标签: ruby-on-rails json active-model-serializers


    【解决方案1】:

    好的,我有一个解决方案。在我的ingreso_serializer.rb 中,我这样定义departamento

    class Api::V1::IngresoSerializer < Api::V1::BaseSerializer
      attributes :field1, :field2, :field3, :field4, :field5, :concepto, :colaborador, :departamento
      def departamento
        object.departamento.as_json(root: false, include: :propietario)
      end
    
      def concepto
        object.concepto.as_json(root: false)
      end
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      相关资源
      最近更新 更多