7

我在 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

使用这样的序列化程序:

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

  has_one :reporter, :embed => :ids
  has_one :assignee, :embed => :ids
end

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name
end

class CommentSerializer < ActiveModel::Serializer
  attributes :id, :body
end

这会生成类似于以下内容的 JSON:

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

问题是 Ember 无法将侧面加载的报告者和受让人 JSON 对象识别为用户对象,并且我看到以下错误:

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 的 embed 方法在其 include 选项中接受了模型列表,则可以过滤 JSON 响应以仅包含侧面加载的注释。
  • Ember 数据的模型可以配置为从“用户”、“记者”和“受让人”侧加载用户......但据我所知,它似乎只支持 sideloadAs 的一个键。
  • 不知何故忽略/禁用未知键的侧面加载错误(可能是最不理智的方法)。

我在这里缺少另一个选择吗?我想我可能需要编写一个修复程序并向 rails-api/active_model_serializers、emberjs/data 或两者提交一个拉取请求。

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

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

  has_many :comments, :embed => :ids
end
4

3 回答 3

4

我有类似的问题,并在ActiveModel::Serializers readme找到了解决方案。您可以使用: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
于 2013-04-02T21:00:57.020 回答
1

你应该阅读这个页面。修订版 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"
    }
  ]
}

因为您在模型中配置了reportertype User,所以 Ember 在结果中搜索用户。

于 2013-03-12T18:55:42.270 回答
0

我遇到了同样的问题,添加include: false关联对我有用。

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

    has_many :domains, key: :domains, include: false
    has_many :sessions, key: :sessions
于 2014-05-08T16:19:26.250 回答