Many to many ActiveModel + rails

Hi People,

I have two models:

App.Emotion = DS.Model.extend name: DS.attr(‘string’) statuses: DS.hasMany ‘status’, { async: true }

App.Status = DS.Model.extend body: DS.attr ‘string’ emotions: DS.hasMany ‘emotion’, { async: true } created_at: DS.attr ‘date’, { readOnly: true } updated_at: DS.attr ‘date’, { readOnly: true }

When I save the status, I’m getting the following result:

{“status”=>{“body”=>“labore et dolore magna aliquyam erat, sed diam voluptua.”, “created_at”=>nil, “updated_at”=>nil, “emotions”=>[{“name”=>“Test”, “statuses”=>nil, “id”=>“a87af1bb-de56-4092-ae52-e21bdd5efc14”}, {“name”=>“Test2”, “statuses”=>nil, “id”=>“117489c3-5ef3-476a-bdd1-8048b10d21be”}, {“name”=>“Test3”, “statuses”=>nil, “id”=>“721f0d0f-f7d5-48f3-94c7-2aa8fe3d2441”}]}}

Rails is expecting emotion_ids whats the best practise for handling many to many relationships in ember?

I added this bit of code, this seems to make it work. Is this an acceptable solution or is there a better way?

App.ApplicationSerializer = DS.ActiveModelSerializer.reopen serializeHasMany: (record, json,    relationship) ->
 key = relationship.key
 relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship)

# Embed hasMany relationship if records exist
if (relationshipType is "manyToNone" or relationshipType is "manyToMany" or relationshipType is "manyToOne")
  json[key.singularize() + '_ids'] = record.get(key).mapBy("id")
# Fallback to default serialization behavior
else
  @_super record, json, relationship

Is there any update to this? Is there a guide for how we should handle many to many associations?

1 Like