1

I have a meeting and a sales-rep models, The relation is ManyToMany.

The problem is, When I want to create a New meeting, and assign existing salesReps to it (They are already saved to the store), But The salesReps IDS are not included in The post action caused by model.save() (not even an empty array), To make it more clear, Here is what my code looks like:

meeting.coffee:

Meeting = DS.Model.extend
  client:     DS.belongsTo('client')
  salesReps:  DS.hasMany('sales-rep')
  memo:       DS.attr('string')
  startDate:  DS.attr('date')
  duration:   DS.attr()

sales-rep.coffee:

SalesRep = DS.Model.extend
  meetings:   DS.hasMany('meeting')
  firstName:  DS.attr('string')
  lastName:   DS.attr('string')
  title:      DS.attr('string')

meetings/new.coffee (the save action am using inside new meeting controller):

save: ->
  meeting = @get('model')
  meeting.set('client', @get('client'))
  meeting.get('salesReps').pushObjects(@get('salesReps.content'))
  meeting.save().then =>
    @transitionToRoute 'meetings'

the JSON payload: ( POST http://localhost:4200/api/meetings)

meeting: {memo: null, start_date: null, duration: "00:15", client_id: null}
client_id: null
duration: "00:15"
memo: null
start_date: null

No matter what, There is no ANY trace of the salesReps ids in the payload!!

What I tried so far:

  • Setting the hasMany relation in the meeting model only.

  • Setting {async: true}, and then {async: false}, on both SalesRep, And then on one of them

  • spending almost 2 days googling and reading all related posts in here with no luck

Any Help/hints/Advice, Is highly appreciated

Dhaulagiri
  • 3,281
  • 24
  • 26
Mawaheb
  • 822
  • 10
  • 21
  • 3
    this issue here https://github.com/emberjs/data/issues/120 might have something to do with it, and possible solution here http://stackoverflow.com/questions/15624193/many-to-many-relationships-with-ember-ember-data-and-rails – Craicerjack Feb 03 '15 at 11:41

1 Answers1

0

I will write the solution I found after endless reading and researching, I will write the full details, and trial/failure i've been through, Because no one, no one EVER should have to spend more than 3 days trying to fix something like that!!

I am using Ember-cli, So, there are a files/directories structure am following:

First attempt:

Trying all combinations of async: true, embedded true and what not.

Result, No luck


Second attempt:

in app/serializers/ I added the following serializer file:

meeting.coffee

`import DS from "ember-data"`
`import Ember from "ember"`
`import config from '../config/environment'`
 get = Ember.get

serializer = DS.RESTSerializer.extend
  serializeHasMany: (record, json, relationship) ->
    rel_ids = get(record, relationship.key).map (rel) -> get(rel, 'id') || []
    json["#{relationship.key.underscore().singularize()}_ids"] = rel_ids
    json
`export default serializer`

result:

Adding this serializer, And I finally was able to send sales_rep_ids:[] array to the controller! and I could confirm that the server is saving the accociations as required.

But, When listing meetings, I was not able to list the associated salesReps, So, I checked the JSON am getting from the server, and it was correct (salesReps Ids were included!) But still not listed in Ember


Third Attempt:

After more reading and endless head-banging-against-the-wall, Changing ONE line fixed the problem!:

in app/serializers/meeting.coffee change serializer = DS.RESTSerializer.extend to

serializer = DS.ActiveModelSerializer.extend

And Voila! Saved to the back-end, And listed correctley as association in ember!

This solution is a result of 3+ of constant headache, Am posting it here hopefully it might be helpful to someone facing the same problem, I can't claim that it's my own solution, but, It's the result of reading many people's code.

am not sure if it's the Ember way to do so, So, Any suggestions, Improvements Ideas and thought are welcome.

Mawaheb
  • 822
  • 10
  • 21