[英]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_one
或belongs_to
,如下所示:
class API::DashboardSerializer < ActiveModel::Serializer
attributes :id, :name
has_one :special, serializer: CheckInSerializer
def special
# ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.