Error loading JSON for tags with tag attributes using active model serializers

In a Ruby 2 / Rails 4 application, I am trying to use act-as-taggable-on along with active_model_serializers to create a JSON API that displays my tags along with other model parameters.

Firstly, there are some prerequisites / motivation for this problem: JSON is loaded into ember / ember-data data, which at the time of writing of this article removed support for embedded records in JSON. The documentation has an alleged fix, but I find it awkward and didn't really get it to work. Since I'm new to Ember, and I'm a little more comfortable with Rails, I believe that I will try to solve the problem in another way by loading the tag entry along with the document entry. I like this solution better because it makes more sense for my application, but I can't get it to work.

Here is an example: let's say I have a document model that uses act-as-taggable-on:

class Document < ActiveRecord::Base acts_as_taggable # other code omitted 

I created a database with one document with one tag. Now consider the following cases:

1. Insert a complete object : with the following serializer:

 class DocumentSerializer < ActiveModel::Serializer attributes :id has_many :tags 

My JSON has the following format (using UIDID Rails 4):

 { "documents": [ { "id": "c41460fa-2427-11e3-8702-0800270f33f4", "tags": [ { "id": "a33fc396-2428-11e3-8eeb-0800270f33f4", "name": "test" } ] } ] } 

2. ID Insert : using serializer

 class DocumentSerializer < ActiveModel::Serializer attributes :id has_many :tags, embed: :id 

Now my JSON looks like this:

 { "documents": [ { "id": "c41460fa-2427-11e3-8702-0800270f33f4", "tag_ids": [ "a33fc396-2428-11e3-8eeb-0800270f33f4" ] } ] } 

3. ID Insert with tags Sideloaded : according to the documentation of active_model_serializers, I should be able to do

 class DocumentSerializer < ActiveModel::Serializer attributes :id has_many :tags, embed: :id, include: true 

but it does not work. Instead, I get a NoMethodError:

 undefined method `object' for #<ActsAsTaggableOn::Tag:0x007f258cf1db38> 

I tried to find this problem, but so far have not found anything useful. I also could not find any gem documentation regarding use with another gem. My suspicion right now is that it has something to do with how the action-like-taggable-on is implemented, that this is not a simple has_many relationship? Can anyone suggest any contribution to this problem? Thanks in advance!

+7
json ruby-on-rails active-model-serializers acts-as-taggable-on
source share
1 answer

Fixed! It turns out that in order to bypass 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 values ​​will be used in the absence of one. Apparently this is not the case if you want to use the include: true option. The key in mind came from here , thanks a lot!

For completeness, I will 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" ] } ] } 
+11
source share

All Articles