使用Rails + active_model_serializers在EmberJS中使用非标准类名称对象加载对象 [英] Side-loading objects with non-standard class names in EmberJS with Rails+active_model_serializers
问题描述
类问题< p>我有一些Rails中的几个模型看起来像这样: ActiveRecord :: Base
belongs_to:reporter,class_name:'User'
belongs_to:assignee,class_name:'User'
has_many:comments
end
类用户ActiveRecord :: Base
end
class注释< ActiveRecord :: Base
end
与序列化器一样:
class IssueSerializer< ActiveModel :: Serializer
属性:id
embed:ids,include:true
has_one:reporter,:embed => :ids
has_one:assignee,:embed => :ids
end
class UserSerializer< ActiveModel :: Serializer
属性:id,:name
end
class CommentSerializer< ActiveModel :: Serializer
属性:id,:body
end
JSON看起来像以下内容:
{
issue:[
{
id:6,
reporter_id:1,
assignee_id:2,
comment_ids:[
3
]
},
],
评论:[
{
id:3
body:好评
}
记者:[
{
id:1
name:Ben Burton
}
]
assignees:[
{
id:2
name:Jono Mallanyk
}
]
}
问题是边框加载的记者和受让人的JSON对象不被Ember识别为用户对象,我看到以下错误:
未捕获错误:断言失败:您的服务器与关键记者返回哈希,但您没有映射
我不想删除
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
属性:id,:reporter_id,:assignee_id
embed:ids,include:true
has_many:comments,:embed => :ids
end
我有类似的问题并在 ActiveModel :: Serializers readme 中找到解决方案。您可以使用:root
选项。尝试这样的问题序列化程序:
类IssueSerializer< ActiveModel :: Serializer
属性:id
embed:ids,include:true
has_one:reporter,:root => users
has_one:assignee,:root => users
has_many:comments
end
I have a few models in Rails that look something like this:
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
with serializers like so:
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
This produces JSON that looks something like the following:
{
"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"
}
]
}
The problem is that the side-loaded reporters and assignees JSON objects aren't recognized by Ember as User objects, and I see the following error:
Uncaught Error: assertion failed: Your server returned a hash with the key reporters but you have no mapping for it
I don't want to remove
embed :ids, include: true
from my IssueSerializer, because then the comments wouldn't be serialized.
There are a few potential ways to solve this that I've considered:
- If the embed method for ActiveModel::Serializer accepted a list of models in its include option, this could filter the JSON response to only include side-loaded comments.
- Ember data's model could be configured to side load users from "users", "reporters" and "assignees"... but as far as I can tell from the source it only appears to support one key for sideloadAs.
- Somehow ignore/disable side-loading errors for unknown keys (probably the least sane approach).
Is there another option I'm missing here? I think I may have to write a fix and submit a pull request to either rails-api/active_model_serializers, emberjs/data, or both.
EDIT: My temporary workaround for this is to change the IssueSerializer to only serialize the ids for reporter and assignee:
class IssueSerializer < ActiveModel::Serializer
attributes :id, :reporter_id, :assignee_id
embed :ids, include: true
has_many :comments, :embed => :ids
end
I had similar problem and have found solution at ActiveModel::Serializers readme. You can use :root
option. Try something like this for issue serializer:
class IssueSerializer < ActiveModel::Serializer
attributes :id
embed :ids, include: true
has_one :reporter, :root => "users"
has_one :assignee, :root => "users"
has_many :comments
end
这篇关于使用Rails + active_model_serializers在EmberJS中使用非标准类名称对象加载对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!