0

我使用ActiveModel :: Serializer來序列化我的json數據。 我有三個型號如下Rails ActiveModel Serializer:檢索深度嵌套的ActiveRecord協會

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" 
     } 
    } 
]** 

所以直接從發票獲取嵌套屬性,我需要改變我的模型之間的關係?

有人遇到同樣的問題,或者你身邊的任何工作都可以建議嗎?

+0

https://stackoverflow.com/questions/32079897/serializing-deeply-nested-associations-with-active-model-serializers –

+0

只有我可以補充:不要全局使用它,使用'render json:data,包括:'**''你實際上需要深層嵌套。 –

+0

感謝那個工作..的人! – Paras

回答

0

如果有人被卡在這個問題,這裏是我做過什麼:

每當我要檢索的深層嵌套的關聯響應我在加入「包括*」關鍵字在控制器中。

例如:

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 

包括*將檢索所有用於使用串行如果用於各個模型中定義的發票模型嵌套屬性。