嵌套包含Rails只显示一个

时间:2014-10-23 18:50:18

标签: ruby-on-rails ruby json

我试图嵌入一些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

1 个答案:

答案 0 :(得分:1)

你需要将它们作为两个单独的哈希发送:

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