【问题标题】:How can you sideload multiple instances of a model with a belongs_to association?你如何通过belongs_to 关联加载模型的多个实例?
【发布时间】:2013-03-25 05:22:10
【问题描述】:

我正在使用 rails / ember / ember-data,并尝试侧载多个关联。

App.Plan = DS.Model.extend({
  schedule: DS.belongsTo('App.Schedule'),
  questions: DS.hasMany('App.Question')
});

App.Schedule = DS.Model.extend({
  plan: DS.belongsTo('App.Plan'),
  start: DS.attr('date'),
});

App.Question = DS.Model.extend({
  name: DS.attr('string')
})

以及以下 Rails 序列化程序:

class PlanSerializer < ActiveModel::Serializer
  embed :ids, :include => true

  attributes :id, :owner_id

  has_one :schedule
  has_many :questions
end

class QuestionSerializer < ActiveModel::Serializer
  attributes :id, :name, :plan_id
end

class ScheduleSerializer < ActiveModel::Serializer
  attributes :id, :start, :time_established, :plan_id
end

我收到以下错误:

Uncaught Error: assertion failed: Your server returned a hash with the 
key schedules but you have no mapping for it

这是指什么“映射”?我正在尝试通过 /plans.json 提取所有信息

如果我注释掉所有日程安排内容,则计划/问题加载正常。但我就是无法让时间表正常运行。

我认为这与我嵌入多个 scheduleS(复数)但计划关联是单数的事实有关。我想这可能与客户端的多元化有关并尝试过:

DS.RESTAdapter.configure("plurals", {
  schedule: 'schedules'
});

...但没有帮助。很确定这不是问题,因为它不是不规则的复数形式。

是不是跟返回的json的顺序有关系?

这个 json 返回时间表、问题,然后是计划。虽然计划是“根”节点。

有什么想法吗?谢谢

【问题讨论】:

    标签: ruby-on-rails serialization ember.js ember-data


    【解决方案1】:
    DS.RESTAdapter.configure('App.Schedule',
      {sideloadAs: 'schedules'}
    );
    

    【讨论】:

      猜你喜欢
      • 2011-08-14
      • 1970-01-01
      • 2011-04-30
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      相关资源
      最近更新 更多