Active Model Serializers data attributes from associations are not loading

不想你离开。 提交于 2019-12-25 06:57:59

问题


I am trying to load the association as follows:

This is my controller which is trying to render the json.

#app/controllers/store/products_controller.rb
  def customize
    @option_types = OptionType.all
    respond_to do |format|
      format.json { render :json => @option_types}
    end
  end

This is the serializer for above object

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

option_value belong_to option_type

#app/serializers/option_value_serializer.rb
class OptionValueSerializer < ActiveModel::Serializer
  attributes :id, :key, :option_type_id, :percentage_price_impact, :fixed_price_impact, :absolute_price_impact, :name
end

This is generating the following JSON. Although a number of other params are specified in the option_value_serializer.rb, only id and type appear in the generated JSON

// http://0.0.0.0:3000/products/810/customize.json
{
  "data": [
    {
      "id": "1",
      "type": "option-types",
      "attributes": {
        "key": "fabric",
        "customizable": true,
        "name": "FABRIC"
      },
      "relationships": {
        "option-values": {
          "data": [
            {
              "id": "1",
              "type": "option-values"
            },
            {
              "id": "2",
              "type": "option-values"
            }
          ]
        }
      }
    },
    {
      "id": "2",
      "type": "option-types",
      "attributes": {
        "key": "cuff-type",
        "customizable": false,
        "name": "CUFF TYPE"
      },
      "relationships": {
        "option-values": {
          "data": [

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

          ]
        }
      }
    }
  ]
}

回答1:


forget the associations, do it this way:

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

  def option_values
    object.option_values.collect do |ov|
      {
          id: ov.id,
          key: ov.key,
          option_type_id: ov.option_type_id,
          percentage_price_impact: ov.percentage_price_impact,
          fixed_price_impact: ov.fixed_price_impact,
          absolute_price_impact: ov.absolute_price_impact,
          name: ov.name,
          description: ov.description,
          image: ov.option_image
      }
    end
  end

end



回答2:


Answer below is correct another approach could be like this:

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

  def option_values
    object.option_values.collect do |ov|
      OptionValueSerializer.new(ov)
    end
  end

end

By using this way you don't need to create object manually you can include decorators in OptionValueSerializer or include more relationship if you have nested one.

Hope this helps.



来源:https://stackoverflow.com/questions/36983596/active-model-serializers-data-attributes-from-associations-are-not-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!