使用活动模型序列化程序序列化 2 级嵌套属性

Serialize 2 level nested attributes using active model serializers

我在序列化期间获取嵌套属性时遇到问题。 下面是我的 OptionType 模型,它有许多关联的 OptionValues。

#app/models/option_type.rb
class OptionType < ActiveRecord::Base
  # Plugins
  translates :name, :description
  accepts_nested_attributes_for :translations, allow_destroy: true, reject_if: :all_blank
  # Associations
  has_many :option_values, inverse_of: :option_type, dependent: :destroy
  # Nested Attributes
  accepts_nested_attributes_for :translations
end

下面是我的 OptionValue 模型,它属于一个 OptionType 并且关联了一个 OptionImage。

#app/models/option_value.rb
class OptionValue < ActiveRecord::Base
  # Plugins
  translates :name, :description
  # Associations
  belongs_to :option_type, inverse_of: :option_values
  has_one :option_image, as: :viewable, inverse_of: :option_value
  # Nested Attributes
  accepts_nested_attributes_for :translations
  accepts_nested_attributes_for :option_image, allow_destroy: true,        
end

下面是属于 OptionValue 的 OptionImage 模型。

#app/models/option_image.rb
class OptionImage < Asset
  # Associations
  belongs_to :option_value, inverse_of: :option_image
end

序列化 OptionType 模型时,我需要有关 option_values 的信息以及相关图像。 下面是我的 OptionTypeSerializer,它返回与之关联的 option_values,但我不知道如何在序列化它时也获取关联的图像。

#app/serializers/option_type_serializer.rb
class OptionTypeSerializer < ActiveModel::Serializer
  attributes :id, :key, :customizable, :name, :option_values

  def option_values
    object.option_values
  end
end

-如何在各自的 JSON 对象中获取 option_value 的图像?

-如何自定义此 json 以仅获取 option_value 中的几个属性,例如名称和 ID?

我试过在 OptionTypeSerializer 中使用通常的 has_many 和 belongs_to,但它不起作用。

下面是此序列化程序返回的 JSON。 // http://0.0.0.0:3000/products/786/customize.json

{
  "data": [
    {
      "id": "1",
      "type": "option-types",
      "attributes": {
        "key": "fabric",
        "customizable": true,
        "name": "FABRIC",
        "option-values": [
          {
            "id": 1,
            "key": "Cotton",
            "name": "Cotton",
            "description": ""
          },
          {
            "id": 2,
            "key": "Linen"
            "description": ""
          },
          {
            "id": 3,
            "key": "polyster",
            "name": "Polyster",
            "description": ""
          },
          {
            "id": 4,
            "key": "egyptian cotton",
            "name": "Egyptian Cotton",
            "description": ""
          }
        ]
      }
    },
    {
      "id": "2",
      "type": "option-types",
      "attributes": {
        "key": "cuff-type",
        "name": "CUFF TYPE",
        "option-values": [

        ]
      }
    },
    {
      "id": "3",
      "type": "option-types",
      "attributes": {
        "key": "vents",
        "name": "VENTS",
        "option-values": [

        ]
      }
    }
  ]
}

您可以添加OptionValueSerializer

 class OptionValueSerializer < ActiveModel::Serializer
   attributes :id
   has_one :option_image
 end

然后在 OptionTypeSerializer 中调用 OptionValueSerializer,例如: #app/serializers/option_type_serializer.rb

class OptionTypeSerializer < ActiveModel::Serializer
  attributes :id, :key, :customizable, :name, :option_values

  has_many :option_values, serializer: OptionValueSerializer
  def option_values
    object.option_values
  end
end

你有选项值序列化程序吗?

在那里,您只需指定一个 属性 和一个可能类似于以下内容的方法:

OptionValueSerializer < ActiveModel::Serializer
  attributes :id, :image

  def image
   object.option_image.url # or whatever methood
  end
end