首页 > 解决方案 > 如何序列化存储的正确字段有一个 has_many through 关系表

问题描述

我有一个 has_many through 关系。但我需要将存储在关系表中的字段序列化为 workflow_state。

为此,我必须直接访问关系表。

这是我拥有的当前序列化程序:

class OrderSerializer < ActiveModel::Serializer
  
  attributes :id, :number, :slug, :quoted, :invoiced, :paid, :reciepted, :void, :refund, :workflow_state
  has_one :table
  has_one :user
  has_one :quote
  has_one :reciept
  has_many :items

  class RecieptSerializer < ActiveModel::Serializer
    attributes :id, :order_number, :number, :slug, :cost, :paid, :vat, :total, :change
  end
  
  class UserSerializer < ActiveModel::Serializer
    attributes :id, :name, :email
  end

  class QuoteSerializer < ActiveModel::Serializer
    attributes :id, :order_number, :number, :slug, :cost, :vat, :total
  end

  class TableSerializer < ActiveModel::Serializer
    attributes :id, :number, :position
  end
  
  def items
    customized_items = []

    object.items.each do |item|
      # Assign object attributes (returns a hash)
      # ===========================================================
      custom_item = item.attributes
      # has_one w/only specified attributes
      custom_item[:category] = item.category.slice(:id, :name, :icon)
      custom_item[:workflow_state] = item.order_items.find_by_item_id(item.id).workflow_state
      customized_items.push(custom_item)
    end
    
    return customized_items
  end
end

问题是它给出了正确的响应类型,但是它显示的字段错误的是所有项目都“创建”。但是当我检查数据库时,该字段正在“准备”。

回复:

{
    "order": {
        "id": 7,
        "number": 7,
        "slug": "61b8a5ad-b3f9-4492-b7d7-2c61684c4988",
        "quoted": true,
        "invoiced": false,
        "paid": false,
        "reciepted": false,
        "void": false,
        "refund": false,
        "workflow_state": "preparing",
        "table": {
            "id": 9,
            "number": 9,
            "position": "Bar"
        },
        "user": {
            "id": 1,
            "name": "admin",
            "email": "admin@admin.com"
        },
        "quote": {
            "id": 7,
            "order_number": 7,
            "number": 7,
            "slug": "43f0ca98-84dc-4e6d-8062-f3f1ef1f2e5f",
            "cost": "22.0",
            "vat": "0.0",
            "total": "22.0"
        },
        "reciept": null,
        "items": [
            {
                "id": 1,
                "name": "Eggs bendict",
                "icon": "breakfast",
                "cost": "5.5",
                "price": "7.5",
                "description": "Eggs on toasted muffins with holindais sauce",
                "category_id": 1,
                "organisation_id": 1,
                "created_at": "2020-06-26T07:45:44.603Z",
                "updated_at": "2020-06-26T07:45:44.603Z",
                "category": {
                    "id": 1,
                    "name": "Breakfast",
                    "icon": "breakfast"
                },
                "workflow_state": "created"
            },
            {
                "id": 1,
                "name": "Eggs bendict",
                "icon": "breakfast",
                "cost": "5.5",
                "price": "7.5",
                "description": "Eggs on toasted muffins with holindais sauce",
                "category_id": 1,
                "organisation_id": 1,
                "created_at": "2020-06-26T07:45:44.603Z",
                "updated_at": "2020-06-26T07:45:44.603Z",
                "category": {
                    "id": 1,
                    "name": "Breakfast",
                    "icon": "breakfast"
                },
                "workflow_state": "created"
            },
            {
                "id": 1,
                "name": "Eggs bendict",
                "icon": "breakfast",
                "cost": "5.5",
                "price": "7.5",
                "description": "Eggs on toasted muffins with holindais sauce",
                "category_id": 1,
                "organisation_id": 1,
                "created_at": "2020-06-26T07:45:44.603Z",
                "updated_at": "2020-06-26T07:45:44.603Z",
                "category": {
                    "id": 1,
                    "name": "Breakfast",
                    "icon": "breakfast"
                },
                "workflow_state": "created"
            },
            {
                "id": 13,
                "name": "Fries",
                "icon": "fries",
                "cost": "5.5",
                "price": "7.5",
                "description": "Fries",
                "category_id": 5,
                "organisation_id": 1,
                "created_at": "2020-06-26T07:45:44.636Z",
                "updated_at": "2020-06-26T07:45:44.636Z",
                "category": {
                    "id": 5,
                    "name": "Sides",
                    "icon": "fries"
                },
                "workflow_state": "created"
            }
        ]
    }
}

我在想的是这样的:

custom_item[:workflow_state] = item.order_items.find_by_item_id(item.id).workflow_state

它可能会序列化 order_items 中指定的项目 id 中的第一个项目。

有没有更好的方法从给定关系的关系表中序列化一个字段?

标签: ruby-on-rails-5

解决方案


我重构了整个应用程序以不使用 has_many :through 关系。


推荐阅读