2014-07-23 2 views
2

이 문제에 대한 다른 해결책을 찾고 있었지만 Rails와 Ember 사이의 다형성 관계를 처리하는 최선의 방법을 찾지 못했습니다. 필자의 경우에는 "todos"라는 다형성 테이블이 있으며이 테이블과의 관계 중 하나가 "patient"입니다. 나는 할 일 기록을 얻지 만 그들은 그들이 관련된 어떤 환자인지 모른다. 이것에 대한 어떤 도움도 크게 감사 할 것입니다.Ember와 Rails의 다형성 관계

레일 모델 :

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 

엠버 데이터 모델 :

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 
+0

은 왜 모두'belongs_to을해야합니까? –

+0

@SergioA. 좋은 캐치. 거기 있으면 안된다. 고맙습니다. – sturoid

답변

2

실제로 (즉, 여러 왜 그렇게 보통의 다 대일 관계입니다.

class Todo < ActiveRecord::Base 
    belongs_to :todoable, polymorphic: true 
end 

class Patient < ActiveRecord::Base 
    has_many :todos, as: :todoable 
end 

는 시리얼 레일 한 환자에게 속할 수 있음) 다형성 관계가 아닙니다. "Todos가 환자 또는 의사 또는 개"에 속할 수 있다고 말하면 다형성이 해답이 될 수 있습니다.

그래서, 당신은 간단하게 수행 할 수 있습니다

class Todo < ActiveRecord::Base 
    belongs_to :patient 
end 

class Patient < ActiveRecord::Base 
    has_many :todos 
end 

그리고 엠버의를 : todoable`와`belongs_to : patient`

App.Todo = DS.Model.extend 
    patient: DS.belongsTo 'patient' 

App.Patient = DS.Model.extend 
    todos: DS.hasMany 'todo', async: true