Note that there are some explanatory texts on larger screens.

plurals
  1. POSave ManyToMany records to rails backend
    primarykey
    data
    text
    <p>I'm having trouble persisting associated records to a Rails backend with ember-data.</p> <p>Models:</p> <pre><code>FP.Student = DS.Model.extend firstName: DS.attr('string') lastName: DS.attr('string') imageUrl: DS.attr('string') room: DS.hasMany('FP.Room') parents: DS.hasMany('FP.Parent') observations: DS.hasMany('FP.Observation') FP.Observation = DS.Model.extend name: DS.attr('string') description: DS.attr('string') observedAt: DS.attr('string') room: DS.belongsTo('FP.Room') educator: DS.belongsTo('FP.Educator') students: DS.hasMany('FP.Student', embedded: true) </code></pre> <p>I want to add a list of pre-existing students from a Select view to a new Observation. Assuming the Student models have be collected in <code>controller.selectedStudents</code> I do:</p> <pre><code> saveObservation: -&gt; console.log "ObservationsController saveObservation" obs = @get('newObservation') # new observation is created previously obs.set('observedAt', new Date) obs.set('room', @get('room')) obs.set('educator', @get('room.educators').objectAt(0)) selected = @findSelectedStudents() obs.get('students').pushObjects(selected) obs.get('transaction').commit() findSelectedStudents: -&gt; @get('selectedStudents').map( (id) =&gt; @get('students').find( (student) -&gt; student.id is id ) ) </code></pre> <p>The resulting json sent back to the server looks like (this is from the server log):</p> <pre><code>Started POST "/observations" for 127.0.0.1 at 2013-04-24 21:04:12 +1000 Processing by ObservationsController#create as JSON Parameters: {"observation"=&gt; {"name"=&gt;"DDS", "description"=&gt;"asdsad", "observed_at"=&gt;"Wed Apr 24 2013 21:04:04 GMT+1000 (EST)", "room_id"=&gt;203, "educator_id"=&gt;535, "students"=&gt;[{"id"=&gt;605, "first_name"=&gt;"Steven", "last_name"=&gt;"Richards", "image"=&gt;"https://www.filepicker.io/api/file/CTUCsDGwdEVaS"}, {"id"=&gt;607, "first_name"=&gt;"Anna", "last_name"=&gt;"Stone", "image"=&gt;"https://www.filepicker.io/api/file/CTUCsDGwdEVaS"}]}} Completed 500 Internal Server Error in 5ms </code></pre> <p>Sorry about the layout, but there are 2 students, with full attribute lists. The error the server throws is <code>ActiveRecord::AssociationTypeMismatch - Student(#70343949384800) expected, got ActiveSupport::HashWithIndifferentAccess(#70343953246680)</code></p> <p>Now, I've got this working with an ajax callback that serializes the students to a <code>student_ids</code> array, containing just the student ids. The server accepts this call and sets up the association. But I'd prefer to use ember-data and not have the hassle of manual record management I've found with a hand-rolled ajax solution.</p> <p>I've tried setting an array of <code>student_ids</code> on the Observation, but then nothing is sent back to the server.</p> <p>I think the current call could work if the association was named <code>student_attributes</code>, but it seems a waste of time to send all the data if the student record isn't dirty.</p> <p>So, should I be trying to override the serializer to send back an array of student_ids? Or am I missing something else?</p> <p>thanks,</p> <p>Martin</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload