Ember-data saving a belongsTo association doesn't send foreign key properly


#1

Short verson: ember data has quit sending the an student_id as the student_id, and is instead sending it as the student. I don’t know if this is a bug or I am doing something wrong:

{"iep"=>{"effective_on"=>"Wed, 20 Nov 2013 06:00:00 GMT", "includes_transportation"=>false, "transportation_trips"=>nil, "transportation_purpose"=>nil, "student"=>"1234"}}

Details version:

I was working on adding an ember app to an existing large rails app back in June, but had to put it down. I’ve picked it up again, but in the bump from Ember V1.0.0-rc.5-80-gf54e8ef to V 1.1.3+pre.e0ffbf84 and Ember-data from V0.13-30 to V1.1.0-beta.3-4 more than a few things have gone haywire.

The current problem is saving a belongsTo association.

App.Router.map (match)->
  @resource 'students', ->
    @resource 'student', path: ':student_id', ->
      @resource 'studentIeps', path: 'ieps', ->
        @route 'new', path: 'new'
        @resource 'studentIep', path: ':iep_id', ->
          @resource 'studentIepGoals', path: 'goals', ->
            @route 'new', path: 'new'

App.StudentIepsRoute = Ember.Route.extend
  model: ->
    @modelFor('student').get('ieps')

App.StudentIepRoute = Ember.Route.extend
  model: (params)->
    @store.find 'iep', params['iep_id']

App.StudentIepsNewRoute = Ember.Route.extend
  setupController: (controller) ->
    controller.set 'content', @store.createRecord('iep')

App.Iep = DS.Model.extend
  effectiveOn: DS.attr('date')
  includesTransportation: DS.attr('boolean')
  transportationTrips: DS.attr('number')
  transportationPurpose: DS.attr('string')

  student: DS.belongsTo('student')

App.Student = DS.Model.extend
  firstName: DS.attr('string')
  lastName: DS.attr('string')
  fullName: Ember.computed ->
    @get("lastName") + ", " + @get("firstName")
  .property("lastName", "firstName")

  ieps: DS.hasMany('iep')

App.StudentIepsNewController = Ember.ObjectController.extend
  needs: ['student']
  createIep: ->
    iep = @store.createRecord('iep')
    iep.set('effectiveOn', new Date(@get('effectiveOn')))
    iep.set('student', @get('controllers.student.model'))

    @set('effectiveOn', '')

    iep.save().then (model) ->
      App.Router.router.transitionTo('studentIep', model)

When the app tries to save an Iep, it hits the server with a POST

{"iep"=>{"effective_on"=>"Wed, 20 Nov 2013 06:00:00 GMT", "includes_transportation"=>false, "transportation_trips"=>nil, "transportation_purpose"=>nil, "student"=>"1234"}}

Which of course bombs with

ActiveRecord::AssociationTypeMismatch in IepsController#create

Student(#70256213944300) expected, got String(#70256139944060)

because ember data has quit sending the student_id as the student_id, and is instead sending it as the student.

What am I missing here?


#2

These bumps are documented in the transition document on Github.

You may be interested in using the ActiveModelSerializer (mentioned in TRANSITION.md) which sends responses more like Ember before the beta release.

Good luck!