使用活动模型序列化程序

时间:2016-05-09 06:44:54

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

我在序列化过程中遇到嵌套属性时遇到了麻烦。 下面是我的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": [

        ]
      }
    }
  ]
}

2 个答案:

答案 0 :(得分:1)

您是否有选项值序列化程序?

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

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

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

答案 1 :(得分:0)

您可以添加 OptionValueSerializer

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

然后在 OptionTypeSerializer 中调用 OptionValueSerializer ,如:   #应用程序/串行化器/ 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