簡體   English   中英

Rails ActiveModel Serializer:檢索深層嵌套的ActiveRecord關聯

[英]Rails ActiveModel Serializer : Retrieving Deeply Nested ActiveRecord Association

我正在使用ActiveModel :: Serializer來序列化我的json數據。 我有三個型號如下

class Invoice < ApplicationRecord
  has_many :invoiceDetails, inverse_of: :invoice
  belongs_to :customer
  accepts_nested_attributes_for :invoiceDetails
end

class InvoiceDetail < ApplicationRecord
  belongs_to :invoice
  belongs_to :product
end

class Product < ApplicationRecord
  belongs_to :company
  belongs_to :category
  belongs_to :user
  has_many :invoice_details
end

序列化器如下:

class InvoiceSerializer < ActiveModel::Serializer
  attributes :id, :total_amount, :balance_amount, :created_at
  belongs_to :customer
  has_many :invoiceDetails
end


class InvoiceDetailSerializer < ActiveModel::Serializer
  attributes :id, :quantity
  belongs_to :product
  belongs_to :invoice
end

class ProductSerializer < ActiveModel::Serializer
  attributes :id, :name, :mrp, :sp, :cp, :stocks, :isPublished
  has_one :category
end

當我檢索發票時,我從關聯的invoiceDetails模型和客戶模型中獲取屬性,但缺少與invoiceDetails模型關聯的產品模型中的屬性。

例如,如果我檢索發票,這是輸出:

[
    {
        "id": 3,
        "total_amount": 450,
        "balance_amount": 350,
        "created_at": "2017-06-27T17:02:20.000Z",
        "customer": {
            "id": 4,
            "company_id": 1,
            "name": "vivek",
            "isActive": true,
            "created_at": "2017-06-27T14:35:50.000Z",
            "updated_at": "2017-06-27T14:35:50.000Z",
            "mobile": "12345678",
            "address": "test",
            "pan_number": null,
            "tin_number": null,
            "party_name": "vipul jwelers"
        },
        "invoiceDetails": [
            {
                "id": 4,
                "quantity": 1
            },
            {
                "id": 5,
                "quantity": 1
            }
        ]
    }
]

但是,如果我直接檢索invoiceDetail,我會得到相關的模型屬性。

**[
    {
        "id": 6,
        "quantity": 5,
        "product": {
            "id": 4,
            "name": "Test Prod",
            "mrp": 150,
            "sp": 130,
            "cp": 100,
            "stocks": 100,
            "isPublished": true
        },
        "invoice": {
            "id": 4,
            "total_amount": 3903,
            "balance_amount": 3,
            "created_at": "2017-07-01T07:45:02.000Z"
        }
    },
    {
        "id": 7,
        "quantity": 10,
        "product": {
            "id": 5,
            "name": "Test Prod 2",
            "mrp": 300,
            "sp": 250,
            "cp": 200,
            "stocks": 10,
            "isPublished": true
        },
        "invoice": {
            "id": 4,
            "total_amount": 3903,
            "balance_amount": 3,
            "created_at": "2017-07-01T07:45:02.000Z"
        }
    }
]**

因此,要直接從發票中檢索嵌套屬性,是否需要更改模型之間的關系?

有人遇到過同樣的問題,或者你周圍的任何工作都可以提出建議嗎?

如果有人遇到這個問題,我就是這樣做的:

每當我想要檢索深層嵌套的關聯時,我在響應期間在控制器中添加“include **”關鍵字

例如 :

def show
    cust_id = params[:customer_id]
    invoice_id = params[:id]
    if cust_id && invoice_id
      invoice = Invoice.where(:id => invoice_id, :customer_id => cust_id)
      render json: invoice, include: '**', status: 200
    else
      render json: { errors: "Customer ID or Invoice ID is NULL" }, status: 422
    end
  end

如果為各個模型定義, include * *將使用序列化程序檢索發票模型的所有嵌套屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM