强制活动模型序列化程序返回关联

时间:2018-03-13 14:47:18

标签: ruby-on-rails ruby-on-rails-4 activerecord serialization active-model-serializers

我有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返回类CheckIn的记录,我希望将CheckInSerailizer用于该记录。如何强制它使用CheckInSerializer中的special

1 个答案:

答案 0 :(得分:1)

special移除attributes,然后尝试this Guide中描述的has_onebelongs_to,如下所示:

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

  has_one :special, serializer: CheckInSerializer

  def special
    # ...
相关问题