Nested Include Rails Only One Shows



我正在尝试嵌套一些as_json包含。然而,只有第一个出现。我的代码在下面。在以下情况下,仅呈现产品。但是,如果切换order_pad和product的顺序,则只有order_pad呈现。

#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

您需要将它们作为两个单独的散列发送:

: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]
        }
      ]
    }
  }
]

相关内容