【问题标题】:Rails ActiveModel Serializer : Retrieving Deeply Nested ActiveRecord AssociationRails ActiveModel 序列化器:检索深度嵌套的 ActiveRecord 关联
【发布时间】:2017-07-04 10:12:05
【问题描述】:

我正在使用 ActiveModel::Serializer 来序列化我的 json 数据。 我有以下三个模型

class Invoice < ApplicationRecord
  has_many :invoiceDetails, inverse_of: :invoice
  belongs_to :customer
  accepts_nested_attributes_for :invoiceDetails
end

class InvoiceDetail < ApplicationRecord
  belongs_to :invoice
  belongs_to :product
end

class Product < ApplicationRecord
  belongs_to :company
  belongs_to :category
  belongs_to :user
  has_many :invoice_details
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
  belongs_to :product
  belongs_to :invoice
end

class ProductSerializer < ActiveModel::Serializer
  attributes :id, :name, :mrp, :sp, :cp, :stocks, :isPublished
  has_one :category
end

当我检索发票时,我从关联的 invoiceDetails 模型和客户模型中获取属性,但与 invoiceDetails 模型关联的产品模型中的属性缺失。

例如,如果我检索发票,这是输出:

[
    {
        "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
            }
        ]
    }
]

但是,如果我直接检索 invoiceDetail,我将获得相关的模型属性。

**[
    {
        "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"
        }
    }
]**

那么,为了直接从发票中检索嵌套属性,我是否需要更改模型之间的关系?

有没有人遇到过同样的问题,或者您可以建议任何解决方法?

【问题讨论】:

  • 只有我可以补充:不要全局使用,在实际需要深度嵌套的地方使用render json: data, include: '**'
  • 感谢工作的人..!!
  • 您能否在此处添加您的问题的答案或评论您必须为其他可能遇到相同问题的人做些什么?

标签: ruby-on-rails activerecord ruby-on-rails-5 activemodel active-model-serializers


【解决方案1】:

如果有人被这个问题困扰,这就是我所做的:

每当我想检索深度嵌套的关联时,我都会在响应期间在控制器中添加 "include **" 关键字

例如:

def show
    cust_id = params[:customer_id]
    invoice_id = params[:id]
    if cust_id && invoice_id
      invoice = Invoice.where(:id => invoice_id, :customer_id => cust_id)
      render json: invoice, include: '**', status: 200
    else
      render json: { errors: "Customer ID or Invoice ID is NULL" }, status: 422
    end
  end

include * * 将使用序列化程序检索发票模型的所有嵌套属性(如果已为各个模型定义)。

【讨论】:

  • 由于字符限制无法编辑您的答案,但可能需要更新您的文本以反映 include ** 而不是 include * 以匹配您的代码块
  • 我不得不删除序列化程序中的 belongs_to 语句,其他我看到“堆栈太深”错误。
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多