这只是一个示例,我知道您通常会有多个评论,但是为了这个示例,假设我们有
以下型号:
models: {
blogPost: Model.extend({
comment: belongsTo(),
}),
picture: Model.extend({
comment: belongsTo(),
}),
comment: Model.extend({
commentable: belongsTo({ polymorphic: true }),
}),
},
和以下工厂:
factories: {
blogPost: Factory.extend({
title: "Whatever",
withComment: trait({
comment: association(),
}),
}),
现在,当尝试使用以下内容播种服务器时:
seeds(server) {
server.create("blogPost", "withComment");
}
它确实播种了它,但是当检查console.log(server.db.dump());
可评论为空时commentableId: null
...。
为什么?
编辑:
这是一个棘手的问题。我变了
comment: Model.extend({
commentable: belongsTo({ polymorphic: true }),
}),
至:
comment: Model.extend({
blogPost: belongsTo({ polymorphic: true }),
}),
只是看看是否commentable
部分导致了问题。这次我得到了一个不同的错误:
Mirage: You're using the association() helper on your comment factory for blogPost, which is a polymorphic relationship. This is not currently supported."
因此,目前无法association()
在多态关系上使用。我希望这在文档中宣布...
尽管如此,即使没有速记,我也找不到播种的方法association()
。