【问题标题】:Can we use polymorphic relationships with Ember data?我们可以对 Ember 数据使用多态关系吗?
【发布时间】:2012-11-05 17:38:05
【问题描述】:

我们有多种类型的“小部件”,可在组中使用,在 Rails 中,我们有一个多态关系来返回所有小部件,无论类型如何。

例如,我们可以调用@group.widgets 来返回所有不同类型的小部件,而不管正在使用什么。

class Group < ActiveRecord::Base
  has_many :group_widgets

  def widgets
    group_widgets.map { |m| m.widget }
  end
end

class GroupWidget < ActiveRecord::Base
  belongs_to :group
  belongs_to :widget, polymorphic: true, dependent: :destroy
end

如果我将 polymorphic => true 添加到序列化程序。例如:

class GroupSerializer < ActiveModel::Serializer
  attributes :id,
             :parent_id,
             :title,
             :group_type

  has_many :widgets, :polymorphic => true
end

我似乎得到了一个递归循环,它以 SystemStackError (stack level too deep) 结束。

我是不是在这里漏掉了一个技巧,或者这只是一些还不能完成的事情?

谢谢, 丹

【问题讨论】:

    标签: ember.js active-model-serializers


    【解决方案1】:

    您是否尝试过在没有多态声明的情况下声明它?

    has_many :widgets
    

    还要确保您的每个小部件类型都有为它们定义的序列化程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多