steventnorris
steventnorris

Reputation: 5896

Nested Include Rails Only One Shows

I am attempting to nest some as_json includes. However, only the first shows up. My code is below. In the below case, only the product is rendered. However, if I switch the order of order_pad and product, only the order_pad renders.

#Renders json
  def as_json(options={})
    super(
        :except => [:created_at, :updated_at, :order_pad_id, :product_id],
          :include => [
              :product => {
                  :except => [:created_at, :updated_at]
              },
              :order_pad => {
                :except => [:created_at, :updated_at, :user_id],
                :include => [
                    :user => {
                      :only => [:id, :name_first, :name_last, :company]
                    }
                ]
              }
          ]
    )
  end

Upvotes: 0

Views: 31

Answers (1)

BroiSatse
BroiSatse

Reputation: 44675

You need to send those as two separate hashes:

:include => [
  {
    :product => {
      :except => [:created_at, :updated_at]
    }
  },
  {
    :order_pad => {
      :except => [:created_at, :updated_at, :user_id],
      :include => [
        :user => {
          :only => [:id, :name_first, :name_last, :company]
        }
      ]
    }
  }
]

Upvotes: 1

Related Questions