使用active-model-serializers为标签为行为的标签侧加载JSON时出错 [英] Error sideloading JSON for act-as-taggable tags using active-model-serializers
问题描述
在Ruby 2/Rails 4应用程序中,我试图将act-as-taggable-on与active_model_serializers结合使用,以创建可将我的标签以及其他模型参数输出的JSON API.
In a Ruby 2/Rails 4 app, I am trying to use acts-as-taggable-on in conjunction with active_model_serializers in order to create a JSON API that would output my tags along with other model parameters.
首先解决此问题的背景/动机:JSON被馈入ember/ember-data,在撰写本文时,它已删除了对JSON中的嵌入式记录的支持. 文档中对此有一个假定的修复程序,但我发现它很笨拙而且还没有真正发挥作用.因为我是Ember的新手,并且对Rails更加满意,所以我认为我将尝试通过另一种方式来解决该问题,即将标签记录与文档记录一起加载.我更喜欢此解决方案,因为它对我的应用程序更有意义,但我也无法使它正常工作.
First some background/motivation for this issue: The JSON is being fed into ember/ember-data, which as of the time of this writing has removed support for embedded records in JSON. There is a supposed fix in the documentation about this, but I find it clumsy and also haven't actually gotten it to work. Since I'm an Ember newbie and am a little more comfortable with Rails, I figure I'll try to solve the problem another way, by sideloading the tag record alongside the document record. I like this solution better because it makes more sense for my application, but I can't get this to work either.
这里有个例子:假设我有一个使用acts-as-taggable-on的Document模型:
Here's an example: Let's say I have a Document model that is using acts-as-taggable-on:
class Document < ActiveRecord::Base
acts_as_taggable
# other code omitted
我已经用一个带有一个标签的文档建立了数据库.现在我们考虑以下情况:
I've set up the database with one document with one tag. Now we consider the following cases:
1.嵌入完整对象:具有以下序列化器:
1. Full Object Embed: With the following serializer:
class DocumentSerializer < ActiveModel::Serializer
attributes :id
has_many :tags
我的JSON具有以下格式(使用Rails 4 UUID):
My JSON has the following format (using Rails 4 UUIDs):
{
"documents": [
{
"id": "c41460fa-2427-11e3-8702-0800270f33f4",
"tags": [
{
"id": "a33fc396-2428-11e3-8eeb-0800270f33f4",
"name": "test"
}
]
}
]
}
2.嵌入ID :使用序列化程序
class DocumentSerializer < ActiveModel::Serializer
attributes :id
has_many :tags, embed: :id
我的JSON现在看起来像:
My JSON now looks like:
{
"documents": [
{
"id": "c41460fa-2427-11e3-8702-0800270f33f4",
"tag_ids": [
"a33fc396-2428-11e3-8eeb-0800270f33f4"
]
}
]
}
3.嵌入标签的ID侧面加载了:根据active_model_serializers文档,我应该可以这样做
3. ID Embed with Tags Sideloaded: According to active_model_serializers documentation, I should be able to do
class DocumentSerializer < ActiveModel::Serializer
attributes :id
has_many :tags, embed: :id, include: true
但这是行不通的.相反,我收到了NoMethodError:
but this doesn't work. Instead, I get a NoMethodError:
undefined method `object' for #<ActsAsTaggableOn::Tag:0x007f258cf1db38>
我已经尝试搜索此问题,但到目前为止没有发现任何有用的信息.我也找不到任何有关与其他gem一起使用的文档.我现在怀疑它与如何实现行为可标记"有关,这不是直接的has_many关系吗?任何人都可以在这个问题上提供一些意见吗?预先感谢!
I've tried searching for this problem but haven't found anything useful so far. I also couldn't find any documentation on either gem regarding usage with the other gem. My suspicion right now that it has something to do with how acts-as-taggable-on is implemented, that it's not a straightforward has_many relationship? Is anybody able to offer some input on this issue? Thanks in advance!
推荐答案
已修复!原来是侧面加载标签,必须定义相应的序列化器.我不知道这一点,因为文档似乎暗示拥有一个序列化程序是可选的,并且在没有序列化程序的情况下会使用一些默认值.如果您希望使用include: true
选项,显然不是这种情况.可见的关键来自此处,非常感谢!
Fixed! Turns out to sideload the tags, the corresponding serializer must be defined. I did not know this, because the documentation seems to imply that having a serializer is optional, and some default will be used in the absence of one. Apparently this is not the case if you wish to use the include: true
option. Key in sight came from here, thanks very much!
为完整起见,我将展示我所做的事情.我用以下代码创建了tag_serializer.rb
:
For completeness, I'll show what I did. I created tag_serializer.rb
with the following code:
module ActsAsTaggableOn
class TagSerializer < ActiveModel::Serializer
attributes :id, :name
end
end
现在是我的JSON:
{
"tags": [
{
"id": "a33fc396-2428-11e3-8eeb-0800270f33f4",
"name": "test"
}
],
"documents": [
{
"id": "c41460fa-2427-11e3-8702-0800270f33f4",
"tag_ids": [
"a33fc396-2428-11e3-8eeb-0800270f33f4"
]
}
]
}
这篇关于使用active-model-serializers为标签为行为的标签侧加载JSON时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!