强制Active Model序列化器返回关联 [英] Force Active Model Serializer to return association

本文介绍了强制Active Model序列化器返回关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动模型序列化器,其具有以下内容:

I have a Active Model Serializer that has the following:

class API::DashboardSerializer < ActiveModel::Serializer
    attributes :id, :name, :special

    def special
        x = object.check_ins.first
        prev = x.prev_ci_with_weigh_in
    end
end

其中,特殊返回类 CheckIn 的记录,我希望该记录使用 CheckInSerailizer 。如何强制它在特殊中使用 CheckInSerializer

where special returns a record of class CheckIn and I'd like it to use the CheckInSerailizer for that record. How can I force it to use the CheckInSerializer in special?

推荐答案

属性中删除​​特殊,然后尝试 has_one 属于,在本指南,例如:

Remove special from attributes and then try has_one or belongs_to, described in this Guide, like this:

class API::DashboardSerializer < ActiveModel::Serializer
  attributes :id, :name

  has_one :special, serializer: CheckInSerializer

  def special
    # ...

这篇关于强制Active Model序列化器返回关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆