Ember.js 使用Rails+;活动\u模型\u序列化程序

Ember.js 使用Rails+;活动\u模型\u序列化程序,ember.js,ember-data,active-model-serializers,Ember.js,Ember Data,Active Model Serializers,我有一些Rails中的模型看起来像这样: class Issue < ActiveRecord::Base belongs_to :reporter, class_name: 'User' belongs_to :assignee, class_name: 'User' has_many :comments end class User < ActiveRecord::Base end class Comment < ActiveRecord::Base end

我有一些Rails中的模型看起来像这样:

class Issue < ActiveRecord::Base
  belongs_to :reporter, class_name: 'User'
  belongs_to :assignee, class_name: 'User'
  has_many :comments
end

class User < ActiveRecord::Base
end

class Comment < ActiveRecord::Base
end
问题是,侧面加载的reporters和assignees JSON对象没有被Ember识别为用户对象,我看到以下错误:

Uncaught Error: assertion failed: Your server returned a hash with the key reporters but you have no mapping for it
我不想搬走

embed :ids, include: true
从我的IssueSerializer,因为这样评论就不会被序列化

我考虑了几种可能的解决方法:

  • 如果ActiveModel::Serializer的嵌入方法在其include选项中接受模型列表,则这可能会过滤JSON响应,使其仅包含侧面加载的注释
  • Ember data的模型可以配置为从“用户”、“报告者”和“受让人”侧面加载用户。。。但就我从源代码中所知,它似乎只支持sideloadAs的一个键
  • 以某种方式忽略/禁用未知密钥的侧加载错误(可能是最不正常的方法)
我还有没有别的选择?我想我可能需要编写一个修复程序,并向rails api/active\u model\u序列化程序、emberjs/data或两者提交一个pull请求

编辑:我的临时解决方法是将IssueSerializer更改为仅序列化报告者和受让人的ID:

class IssueSerializer < ActiveModel::Serializer
  attributes :id, :reporter_id, :assignee_id
  embed :ids, include: true

  has_many :comments, :embed => :ids
end
class IssueSerializer:id
结束
您应该阅读第页。第12版的章节解释了相同类型数据的侧向加载:

现在,homeAddress和workAddress预计将被侧加载 一起作为地址,因为它们是相同的类型。此外 默认的根命名约定(下划线和小写)现在将 也可以应用于侧加载的根名称

您的模型应该是:

App.Issue  = DS.Model.extend({
  reporter: DS.belongsTo('App.User'),
  assignee: DS.belongsTo('App.User'),
  comments: DS.hasMany('App.Comment')
});
JSON结果应该为用户提供一个密钥:

{
  "issues": [
    {
      "id": 6,
      "reporter_id": 1,
      "assignee_id": 2,
      "comment_ids": [
        3
      ]
    },
  ],
  "comments": [
    {
      "id": 3
      "body": "Great comment"
    }
  ],
  "users": [
    {
      "id": 1
      "name": "Ben Burton"
    },{
      "id": 2
      "name": "Jono Mallanyk"
    }
  ]
}

由于您在模型中配置了
reporter
类型为
User
,因此余烬将在结果中搜索一个用户。

我遇到了类似的问题,并在中找到了解决方案。您可以使用
:root
选项。对于问题序列化程序,请尝试以下操作:

class IssueSerializer < ActiveModel::Serializer
  attributes :id
  embed :ids, include: true

  has_one :reporter, :root => "users"
  has_one :assignee, :root => "users"
  has_many :comments
end
class IssueSerializer“用户”
有一个:受让人,:root=>“用户”
有很多评论
结束

我也遇到了同样的问题,在关联上添加了
include:false
对我起了作用

    embed :ids, include: true
    attributes :id, :title

    has_many :domains, key: :domains, include: false
    has_many :sessions, key: :sessions

您能展示您的问题模型吗?下面的回答是正确的。您使用的是什么版本的Ember数据?Ember期望“用户”作为侧面加载的用户模型集合的约定与active_Model_serializers约定不一致,即对象嵌入在顶级字段名中。如果它能在“users”下侧向加载用户对象,那就太好了,但事实并非如此。你还存在另一个例外的问题吗?我目前的解决方案是根本不通过序列化问题中的ID来侧向加载用户(参见上面的编辑)。这显然不是很好,因为如果用户模型不在商店中,则需要第二个请求。在考虑了一会儿之后,我认为余烬的做法是完全合理的。我认为活动的\u模型\u序列化程序有一个(可能?)缺陷,它基于别名而不是类名序列化模型。这会清除前面加载的对象,因此如果有一些用户报告器不是被分配者,那么它将无法工作。尽管如此,了解这一点还是有用的。
class IssueSerializer < ActiveModel::Serializer
  attributes :id
  embed :ids, include: true

  has_one :reporter, :root => "users"
  has_one :assignee, :root => "users"
  has_many :comments
end
    embed :ids, include: true
    attributes :id, :title

    has_many :domains, key: :domains, include: false
    has_many :sessions, key: :sessions