ruby-on-rails-5 - 如何序列化存储的正确字段有一个 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 中的第一个项目。
有没有更好的方法从给定关系的关系表中序列化一个字段?
解决方案
我重构了整个应用程序以不使用 has_many :through 关系。
推荐阅读
- python - SQLAlchemy:子类模型数据库条目获取其他子类模型的属性
- python - 具有多个条件的 Django Q 对象查询过滤失败
- amazon-web-services - 何时在 AWS 中设置非默认 VPC?
- android - Android 中的多个公共 Firebase 数据库实例
- node.js - 在 linux 共享主机帐户内容节点应用程序和 Wordpress 网站中,使用主域的 WordPress 网站和使用子域的节点应用程序是可能的
- angular - Angular 4中同一行中的多个数据
- javascript - AJAX POST 返回 404 Spring
- mysql - DATEDIFF(CURRENT_DATE, updated_at) AS Diff, COUNT(*)
- javascript - 组合数组,但如果找到“名称”,则组合/添加数据
- java - 如何使用来自 android studio 的 POST 请求将 mp3 文件发送到烧瓶服务器