我在使用 ember-data 将关联记录保存到 Rails 后端时遇到问题。
楷模:
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)
我想将选择视图中的预先存在的学生列表添加到新的观察中。假设学生模型已经收集在controller.selectedStudents
我做:
saveObservation: ->
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: ->
@get('selectedStudents').map( (id) =>
@get('students').find( (student) ->
student.id is id
)
)
发送回服务器的结果 json 如下所示(来自服务器日志):
Started POST "/observations" for 127.0.0.1 at 2013-04-24 21:04:12 +1000
Processing by ObservationsController#create as JSON
Parameters: {"observation"=>
{"name"=>"DDS", "description"=>"asdsad", "observed_at"=>"Wed Apr 24 2013 21:04:04 GMT+1000 (EST)", "room_id"=>203, "educator_id"=>535,
"students"=>[{"id"=>605, "first_name"=>"Steven", "last_name"=>"Richards", "image"=>"https://www.filepicker.io/api/file/CTUCsDGwdEVaS"},
{"id"=>607, "first_name"=>"Anna", "last_name"=>"Stone", "image"=>"https://www.filepicker.io/api/file/CTUCsDGwdEVaS"}]}}
Completed 500 Internal Server Error in 5ms
很抱歉布局,但有 2 名学生,有完整的属性列表。服务器抛出的错误是ActiveRecord::AssociationTypeMismatch - Student(#70343949384800) expected, got ActiveSupport::HashWithIndifferentAccess(#70343953246680)
现在,我使用 ajax 回调将学生序列化为一个student_ids
数组,其中仅包含学生 ID。服务器接受此调用并建立关联。但我更喜欢使用 ember-data,而不是像手动 ajax 解决方案那样遇到手动记录管理的麻烦。
我已经尝试在 Observation 上设置一个数组student_ids
,但是没有任何东西被发送回服务器。
我认为如果关联被命名,当前调用可能会起作用student_attributes
,但如果学生记录不脏,发送所有数据似乎是浪费时间。
那么,我是否应该尝试覆盖序列化程序以发回一组 student_ids?还是我错过了其他东西?
谢谢,
马丁