【问题标题】:Serialize last record of a one-to-many association序列化一对多关联的最后一条记录
【发布时间】:2019-12-23 03:31:31
【问题描述】:

我有一个用户在 Height 模型中记录了许多高度(身高测量值),但想仅序列化最后一个高度。

我试图创建一个(假的)自定义 has_one 关联,但它没有给我我想要的东西......

app/serializers/user_serializer.rb

class UserSerializer < BaseSerializer
  attributes :email

  # has_many :heights
  has_one :current_height, record_type: :height do |user|
    user.heights.last
  end
end

app/controllers/users_controller.rb

options[:include] = [:heights]
render json: UserSerializer.new(user, options).

按原样,我得到了错误:"heights is not specified as a relationship on UserSerializer."

如果我取消注释 # has_many :heights,我会得到:

{
    "data": {
        "id": "1",
        "type": "user",
        "attributes": {
            "email": "fake@email.com"
        },
        "relationships": {
            "heights": {
                "data": [
                    {
                        "id": "1",
                        "type": "height"
                    },
                    {
                        "id": "2",
                        "type": "height"
                    }
                ]
            },
            "currentHeight": {
                "data": {
                    "id": "2",
                    "type": "height"
                }
            }
        }
    },
    "included": [
        {
            "id": "1",
            "type": "height",
            "attributes": {
                "value": "186.0"
            }
        },
        {
            "id": "2",
            "type": "height",
            "attributes": {
                "value": "187.0"
            }
        }
    ]
}

但我不想在复合文档中包含所有记录的高度...

预期结果

{
    "data": {
        "id": "1",
        "type": "user",
        "attributes": {
            "email": "fake@email.com"
        },
        "relationships": {
            "currentHeight": {
                "data": {
                    "id": "2",
                    "type": "height"
                }
            }
        }
    },
    "included": [
        {
            "id": "2",
            "type": "height",
            "attributes": {
                "value": "187.0"
            }
        }
    ]
}

【问题讨论】:

    标签: ruby-on-rails active-model-serializers json-api fastjsonapi


    【解决方案1】:

    如果我指责你想多了,请原谅我,但你不能给你的用户添加一个方法来获得最后一个高度吗?

    class User < ApplicationRecord
      #
      # your user code here
      # ...
    
      def last_height
        self.heights.last
      end
    end
    

    然后用current_user.last_height获取记录

    如果这不是您的要求,我很抱歉,我可能没有完全理解这个问题。

    【讨论】:

    • 我刚刚编辑了我的问题以更好地解释预期结果:我想在序列化期间过滤关联关系。据我了解,您的提议只会像这样简化我的代码:has_one :last_height, record_type: :height do |user| user.last_height end 但不能解决我的问题...
    【解决方案2】:

    您可以将此添加到User 模型

    has_one :last_height, -> { order 'created_at DESC' }, class_name: "Height"
    

    【讨论】:

    • 我刚刚试了一下,它产生了这个错误:wrong number of arguments (given 3, expected 1..2)
    猜你喜欢
    • 2014-06-30
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多