title: Rails ActiveModel Serializer : Retrieving Deeply Nested ActiveRecord Association
body: <p>I'm using <strong>ActiveModel::Serializer</strong> to serialize my json data.
  I have three models as follows</p>
  
  <pre><code>class Invoice &lt; ApplicationRecord
    has_many :invoiceDetails, inverse_of: :invoice
    belongs_to :customer
    accepts_nested_attributes_for :invoiceDetails
  end
  
  class InvoiceDetail &lt; ApplicationRecord
    belongs_to :invoice
    belongs_to :product
  end
  
  class Product &lt; ApplicationRecord
    belongs_to :company
    belongs_to :category
    belongs_to :user
    has_many :invoice_details
  end
  </code></pre>
  
  <p><strong>The serializers are as follows :</strong></p>
  
  <pre><code>class InvoiceSerializer &lt; ActiveModel::Serializer
    attributes :id, :total_amount, :balance_amount, :created_at
    belongs_to :customer
    has_many :invoiceDetails
  end
  
  
  class InvoiceDetailSerializer &lt; ActiveModel::Serializer
    attributes :id, :quantity
    belongs_to :product
    belongs_to :invoice
  end
  
  class ProductSerializer &lt; ActiveModel::Serializer
    attributes :id, :name, :mrp, :sp, :cp, :stocks, :isPublished
    has_one :category
  end
  </code></pre>
  
  <p><strong>When I retrieve an invoice I get the attributes from the associated invoiceDetails model and customer model but the attributes from the product model associated with the invoiceDetails model are missing.</strong></p>
  
  <p>For example if I retrieve an invoice, this is the output :</p>
  
  <pre><code>[
      {
          "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
              }
          ]
      }
  ]
  </code></pre>
  
  <p><strong>However if I retrieve invoiceDetail directly I get the associated model attributes.</strong></p>
  
  <pre><code>**[
      {
          "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"
          }
      }
  ]**
  </code></pre>
  
  <p>So for retrieving the nested attributes directly from invoice, do I need to change the relationship among my models?</p>
  
  <p>Has someone encountered the same problems, or any work around you can suggest?</p>