繁体   English   中英

强制Active Model序列化器返回关联

[英]Force Active Model Serializer to return association

我有一个Active Model序列化器 ,具有以下内容:

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

其中special返回一个类CheckIn的记录,我希望它对该记录使用CheckInSerailizer 如何强制它special使用CheckInSerializer

attributes删除special belongs_to ,然后尝试本指南中所述的has_onebelongs_to ,如下所示:

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

  has_one :special, serializer: CheckInSerializer

  def special
    # ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM