Multiple belongsTo/hasManys for the same type

I was hoping someone could shed some light on how multiple belongsTo hasManys for the same type are implemented in the backend for example with active model serializers.

Take the example from the Ember data api that is given for hasMany under explicit inverses.

var belongsTo = DS.belongsTo,
hasMany = DS.hasMany;

App.Comment = DS.Model.extend({
  onePost: belongsTo('post'),
  twoPost: belongsTo('post'),
  redPost: belongsTo('post'),
  bluePost: belongsTo('post')
});

App.Post = DS.Model.extend({
  comments: hasMany('comment', {
    inverse: 'redPost'
  })
});

How would the post or comment endpoint be set up? Also I’m assuming you can further specify other inverse relationships like

App.Post = DS.Model.extend({
   comments: hasMany('comment', {
      inverse: 'redPost'
   }),
   awesomeComments: hasMany('comment', {
      inverse: 'bluePost'
   })
});

I’m terribly sorry if this is really obvious. I’ve been looking around everywhere for answer for this kind of thing and it’s driving me nuts. I’m so used to programming rails and am trying to move over to building the front end with Ember using a rails web api on the back and am a little lost.

Have you tried just the normal JSON format and had problems getting it to work? For example, for the ActiveModelAdapter, I believe it will expect this for a Comment:

{
  comment: {
    one_post: { <Post JSON here> },
    two_post: { <Post JSON here> },
    ...
  }
}

Thank you for your reply. I appreciate it.

Yeah I realised I was being a bit dumb and hadn’t read the active model serializers (AMS) documentation properly.

My issue was I wasn’t sure how to map the inverse relationships from AMS to Ember models when loading the data in from a posts endpoint get request. I should have been specifying a different root for the various different belongs_to associations. Using the example above. In AMS I should have used

class PostSerializer < ActiveModel::Serializer
  embed :ids, include: true

  attributes :attribute1, :attribute2 etc....
  
  has_one :one_post, root: :comment
  has_one :two_post, root: :comment
  has_one :red_post, root: comment
  has_one :blue_post, root: comment
end

If you don’t specify the root then ember gets complains about missing models (e.g. onePost, twoPost etc) which is fair enough as they don’t exist but AMS is telling ember that they do.

Note that AMS doesn’t have a notion of belongs_to and uses has_one instead. Serializers are only concerned with multiplicity, and not ownership. belongs_to ActiveRecord associations can be included using has_one in your serializer. Again, taken straight from the AMS documentation.