Error sideloading JSON for act-as-taggable tags using active-model-serializers

无人久伴 提交于 2019-12-03 13:37:45
xph

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!

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

and now my JSON:

{
   "tags": [
      {
         "id": "a33fc396-2428-11e3-8eeb-0800270f33f4",
         "name": "test"
      }
   ],
   "documents": [
      {
         "id": "c41460fa-2427-11e3-8702-0800270f33f4",
         "tag_ids": [
            "a33fc396-2428-11e3-8eeb-0800270f33f4"
         ]
      }
   ]
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!