如何通过关系 table 序列化存储在具有 has_many 上的正确字段
How to serialze the correct fields that are stored on has a has_many through relation table
我有一个 has_many 直通关系。但是我需要将存储在关系 table 上的字段序列化为 workflow_state.
为此,我必须直接访问关系 table。
这是我当前的序列化程序:
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
问题是它给出了正确的响应类型,但是它显示的字段是错误的,所有项目都显示 'created'。但是当我检查数据库时,该字段是“准备中”。
回复:
{
"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
它可能正在使用指定的项目 ID 序列化 order_items 中的第一个项目。
对于给定关系,是否有更好的方法从关系 table 序列化字段?
我重构了整个应用程序以不使用 has_many :through 关系。
我有一个 has_many 直通关系。但是我需要将存储在关系 table 上的字段序列化为 workflow_state.
为此,我必须直接访问关系 table。
这是我当前的序列化程序:
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
问题是它给出了正确的响应类型,但是它显示的字段是错误的,所有项目都显示 'created'。但是当我检查数据库时,该字段是“准备中”。
回复:
{
"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
它可能正在使用指定的项目 ID 序列化 order_items 中的第一个项目。
对于给定关系,是否有更好的方法从关系 table 序列化字段?
我重构了整个应用程序以不使用 has_many :through 关系。