【发布时间】:2015-06-09 13:17:46
【问题描述】:
在我的应用程序中,我有 BlogPost 模型和 User 模型,它们通过名为 author 的关系相关联。为了提供来自我的 Rails 应用程序的数据,我使用 active_model_serializers 定义:
class Blog::PostSerializer < ActiveModel::Serializer
embed :ids, include: true
attributes :id, :title, :text, :created_at, :updated_at
has_one :author
has_many :assets
end
当我使用 Ember 模型获取这个时:
Admin.BlogPost = DS.Model.extend({
author: DS.belongsTo('User'),
title: DS.attr('string'),
text: DS.attr('string'),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date')
});
有一个错误:
Uncaught Error: Assertion Failed: You looked up the 'author' relationship on a 'blog.post' with id 1 but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (`DS.belongsTo({ async: true })`)
这是因为我的回复看起来像:
{
'blog_posts': [
{
id: 1,
author_id: 1
},
// …
],
'authors': [
{ id: 1, /* … */ }
]
}
有什么方法可以更改'authors' 以响应'users' 或在序列化程序中使用'authors' 作为'users' 的别名?
【问题讨论】:
标签: ruby-on-rails ember.js ember-data active-model-serializers