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
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!