强制活动模型序列化器与 return 关联
Force Active Model Serializer to return association
我有一个 Active Model Serializer 具有以下内容:
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
returns class CheckIn
的记录,我希望它将 CheckInSerailizer
用于该记录。我怎样才能强制它在 special
中使用 CheckInSerializer
?
从 attributes
中删除 special
,然后尝试 has_one
或 belongs_to
,如 this Guide 中所述,如下所示:
class API::DashboardSerializer < ActiveModel::Serializer
attributes :id, :name
has_one :special, serializer: CheckInSerializer
def special
# ...
我有一个 Active Model Serializer 具有以下内容:
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
returns class CheckIn
的记录,我希望它将 CheckInSerailizer
用于该记录。我怎样才能强制它在 special
中使用 CheckInSerializer
?
从 attributes
中删除 special
,然后尝试 has_one
或 belongs_to
,如 this Guide 中所述,如下所示:
class API::DashboardSerializer < ActiveModel::Serializer
attributes :id, :name
has_one :special, serializer: CheckInSerializer
def special
# ...