Note that there are some explanatory texts on larger screens.

plurals
  1. POError sideloading JSON for act-as-taggable tags using active-model-serializers
    primarykey
    data
    text
    <p>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.</p> <p>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 <a href="https://github.com/emberjs/data/blob/master/TRANSITION.md#embedded-records">documentation</a> 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.</p> <p>Here's an example: Let's say I have a Document model that is using acts-as-taggable-on:</p> <pre><code>class Document &lt; ActiveRecord::Base acts_as_taggable # other code omitted </code></pre> <p>I've set up the database with one document with one tag. Now we consider the following cases:</p> <p><strong>1. Full Object Embed</strong>: With the following serializer:</p> <pre><code>class DocumentSerializer &lt; ActiveModel::Serializer attributes :id has_many :tags </code></pre> <p>My JSON has the following format (using Rails 4 UUIDs):</p> <pre><code>{ "documents": [ { "id": "c41460fa-2427-11e3-8702-0800270f33f4", "tags": [ { "id": "a33fc396-2428-11e3-8eeb-0800270f33f4", "name": "test" } ] } ] } </code></pre> <p><strong>2. ID Embed</strong>: Using the serializer</p> <pre><code>class DocumentSerializer &lt; ActiveModel::Serializer attributes :id has_many :tags, embed: :id </code></pre> <p>My JSON now looks like:</p> <pre><code>{ "documents": [ { "id": "c41460fa-2427-11e3-8702-0800270f33f4", "tag_ids": [ "a33fc396-2428-11e3-8eeb-0800270f33f4" ] } ] } </code></pre> <p><strong>3. ID Embed with Tags Sideloaded</strong>: According to active_model_serializers documentation, I should be able to do</p> <pre><code>class DocumentSerializer &lt; ActiveModel::Serializer attributes :id has_many :tags, embed: :id, include: true </code></pre> <p>but this doesn't work. Instead, I get a NoMethodError: </p> <pre><code>undefined method `object' for #&lt;ActsAsTaggableOn::Tag:0x007f258cf1db38&gt; </code></pre> <p>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!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload