0

我有三個車型分別是發票InvoiceDetail產品。 的發票有許多InvoiceDetailsInvoiceDetails所屬的發票和產品Rails的活動模型串行 - 添加嵌套屬性2級模型

我分別對所有三種型號定義序列化,但是,當我取發票,我無法獲取產品屬性。

發票型號:

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

InvoiceDetai型號

class InvoiceDetail < ApplicationRecord 
    belongs_to :invoice 
    belongs_to :product 
end 

產品型號

class Product < ApplicationRecord 
    belongs_to :company 
    belongs_to :category 
    belongs_to :user 
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, :discount, :subtotal 
    belongs_to :product 
end 

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

當我取發票: 的JSON輸出不包括產品屬性。

[ 
    { 
     "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": "xyz" 
     }, 
     "invoiceDetails": [ 
      { 
       "id": 4, 
       "quantity": 1, 
       "discount": 0, 
       "subtotal": 150 
      }, 
      { 
       "id": 5, 
       "quantity": 1, 
       "discount": 0, 
       "subtotal": 300 
      } 
     ] 
    } 
] 
+0

有產品'無關聯'和'invoice_detail'這可能是原因。 – Pavan

+0

它們之間有一個關聯,invoice_detail屬於產品以及發票 – Paras

+1

這還不夠!我建議你閱讀協會! – Pavan

回答

0

AMS不包括關聯關聯 - 它只有1層深。有辦法解決此問題:

  • 使用include在你的控制器(docs
  • 使用像this one一個解決辦法 - 我目前使用它成功地在生產