本文介绍了强制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序列化器返回关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!