0

にネストされた属性を追加するには、私は三つのモデル、すなわち請求書InvoiceDetailと製品を持っています。 が請求書は、多くのInvoiceDetailsInvoiceDetailsがRailsのアクティブなモデルシリアライザ - 第二レベルのモデル

を請求し、製品に属している私は、それぞれ3つのすべてのモデルのために定義されたシリアライザを持っているが、私は、請求書を取得していたとき、私は、製品の属性を取得することはできませんよ。

請求書モデル:

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のような回避策を使用します - 私は現在、製品 `の間には関連がありません生産に成功し
関連する問題