Rails AMS,在多态列表中指定不同的序列化器

Rails AMS, specify different serializers in polymorphic list

我使用 pg_search 实现了多 table 搜索,然后使用 Active Model Serializer(版本 0.10)序列化结果 - 它工作正常,但 AMS 为每个搜索使用默认序列化器返回的类型。

这是序列化程序:

class SearchBarSerializer < ApplicationSerializer
  attributes :searchable_type
  belongs_to :searchable
end

因此,例如,当序列化从 pg_search 返回的对象时,如果相关对象是 "User",则 AMS 使用 UserSerializer。如果相关类型是联赛,则 AMS 使用 LeagueSerializer。

很好 - 但我想为每种类型使用不同的序列化程序。这是一个搜索栏,所以我只关心比完整的标准序列化程序少得多的数据。 (编辑:标准序列化程序为每个 User 和 League 模型序列化所有属性和关联,如下所示。每个模型都有些大,并且出于搜索目的,我真的只需要每个模型的名称和id,以及每种类型的一些其他较小的数据)

有什么方法可以根据对象指定要使用的序列化程序吗?

谢谢!

编辑:

用户模型:

class User < ActiveRecord::Base
  include PgSearch

  #################### Associations
  has_and_belongs_to_many :roles
  belongs_to :profile_page_visibility, optional: true # the optional part is just for when user's are created.
  has_and_belongs_to_many :leagues, class_name: "Leagues::League", join_table: "users_leagues_leagues"
  has_one :customer, class_name: "Payments::Customer", dependent: :destroy
  has_many :unpaid_charges, class_name: "Payments::UnpaidCharge", dependent: :destroy
  has_many :charges, class_name: "Payments::Charge", dependent: :destroy
  has_many :cards, class_name: "Payments::Card", dependent: :destroy
  has_many :league_join_requests, class_name: "Leagues::JoinRequest", dependent: :destroy
  has_many :notifications, class_name: "Notification", foreign_key: :recipient_id
  has_many :league_invitations, class_name: "Leagues::Invitation", dependent: :destroy
  has_many :teams, class_name: "Leagues::Team"
  has_many :divisions, class_name: "Leagues::Division" # Can act as division commissioner
  has_many :conferences, class_name: "Leagues::Conference" # Can act as conference commissioner

联赛模型:

class Leagues::League < ApplicationRecord
  enum pay_level: [ :basic, :custom, :premium ]
  include PgSearch

  #################### Associations
  has_and_belongs_to_many :users, class_name: "User", join_table: "users_leagues_leagues"
  has_and_belongs_to_many :commissioners, class_name: "User", join_table: "commissioners_leagues_leagues"
  belongs_to :commissioner, class_name: "User", foreign_key: :commissioner_id, optional: true
  has_and_belongs_to_many :feature_requests, class_name: "FeatureRequest", join_table: "feature_requests_leagues_leagues"
  has_many :join_requests, class_name: "Leagues::JoinRequest", dependent: :destroy
  has_many :invitations, class_name: "Leagues::Invitation", dependent: :destroy
  has_many :notifications, class_name: "Notification", as: :notifiable_subject, dependent: :destroy
  has_many :teams, class_name: "Leagues::Team", dependent: :destroy
  has_many :conferences, class_name: "Leagues::Conference", dependent: :destroy
  has_many :divisions, class_name: "Leagues::Division", dependent: :destroy

如果您想为您的关联定义一个特定的序列化器查找,您可以将 ActiveModel::Serializer.serializer_for 方法重写为 return 一个 序列化器 class 基于定义的条件。

对于您的情况,它可能类似于:

class SearchBarSerializer < ApplicationSerializer
  attributes :searchable_type
  belongs_to :searchable

  class << self
    def serializer_for(model, options)
      return TinyUserSerializer if model.class == User
      return TinyLeagueSerializer if model.class == Leagues::League

      super
    end
  end
end