Force Active Model Serializer to return association

只谈情不闲聊 提交于 2019-12-13 03:45:41

问题


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

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?


回答1:


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
    # ...


来源:https://stackoverflow.com/questions/49259173/force-active-model-serializer-to-return-association

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!