我一直在寻找不同的解决方案来解决这个问题,但是我似乎找不到任何结论可以最好地解决Rails和Ember之间的多态关系。在我的情况下,我有一个名为“ todos”的多态表,与该表的关系之一称为“患者”。我有待办事项记录,但他们不知道与之相关的患者。任何帮助,将不胜感激。
铁路模型:
class Todo < ActiveRecord::Base
belongs_to :todoable, polymorphic: true
end
class Patient < ActiveRecord::Base
has_many :todos, as: :todoable
end
铁路塞里亚尔:
class TodoSerializer < ActiveModel::Serializer
attributes :id, :content, :todoable_id, :todoable_type
end
class PatientSerializer < ActiveModel::Serializer
attributes :id, :first_name, :last_name, :email
has_many :todos
embed :ids, include: true
end
EMBER数据模型:
App.Todo = DS.Model.extend
todoable_id: DS.attr 'number'
todoable_type: DS.attr 'string'
content: DS.attr 'string'
patient: DS.belongsTo 'patient'
App.Patient = DS.Model.extend
firstName: DS.attr 'string'
lastName: DS.attr 'string'
email: DS.attr 'string'
todos: DS.hasMany 'todo', polymorphic: true, async: true
这实际上是正常的多对一关系(即,许多Todos可以属于一个患者),而不是多态关系。如果您说“ Todos可以属于患者,医生或狗”,那么多态性可能就是答案。
因此,您可以简单地执行以下操作:
class Todo < ActiveRecord::Base
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :todos
end
在Ember中:
App.Todo = DS.Model.extend
patient: DS.belongsTo 'patient'
App.Patient = DS.Model.extend
todos: DS.hasMany 'todo', async: true
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句