【问题标题】:How return the type of polymorphic relationship?如何返回多态关系的类型?
【发布时间】:2017-05-25 23:39:55
【问题描述】:

我有一个participant 模型,它返回一个名为participable 的多态关系,我需要返回关系类型而不是participable 类型。我在前端使用 Ember。

class ParticipantSerializer < ApplicationSerializer # :nodoc:
  attributes :id

  belongs_to :dispute
  belongs_to :participable, polymorphic: true
end

【问题讨论】:

  • participant.participable_type
  • attributes :id, :participable_type

标签: ruby-on-rails ember.js active-model-serializers


【解决方案1】:

就我而言,问题不在于多态关联,而在于应用逻辑。

我通过participants 修复它创建另一个关联:

has_many :participants
has_many :users, through: :participants, source: :participable, source_type: 'User'
has_many :mediators, through: :participants, source: :participable, source_type: 'Mediator'

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 2020-07-31
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2018-12-15
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多