title: How to write activemodel serializer for many to many relationship?
body: <p>Trying to set up the backend for an ember-cli app. Here's how the models look like in Ember:</p>
  
  <p><strong>post.js</strong></p>
  
  <pre><code>export default DS.Model.extend({
    heading: DS.attr('string'),
    content: DS.attr(''),
    imageUrl: DS.attr('string'),
    pageId: DS.belongsTo('page'),
    tagIds: DS.hasMany('tag')
  });
  </code></pre>
  
  <p><strong>tag.js</strong></p>
  
  <pre><code>export default DS.Model.extend({
    name: DS.attr('string'),
    postIds: DS.hasMany('post')
  });
  </code></pre>
  
  <p>The models in Rails and Active Record are just Post, Tag, and Theme. Theme joins Post and Tag. (ie: Post has_many :tags, through: :themes)</p>
  
  <p>Here's what my serializers look like:</p>
  
  <pre><code>class PostSerializer &lt; ActiveModel::Serializer
    embed :ids, include: true
  
    attributes :id, :heading, :content, :image_url
  
    has_many :tags
  end
  
  class TagSerializer &lt; ActiveModel::Serializer
    embed :ids, include: true
  
    attributes :id, :name
  end
  </code></pre>
  
  <p>This works in one direction: searching posts will get all the tags as well. Doesn't work in the other because I don't have a has_many in the TagSerializer. However, if I put a has_many in both serializers, there will be a stack level too deep error. </p>
  
  <p>So I guess my question is: What is the typical way to implement a many-to-many relationship with ActiveModel serializer? I can't seem to find any resources on how to set this up in a Rails back end. Any help would be appreciated. Thanks! </p>