2

저는 루비/레일에서 다형 다발 - 대 - 다 모델 작업을하는 데 문제가 있습니다. 이 모델은 세 가지, 약물에 합류 감염 될 필요가 테이블 및 증상이 있습니다 증상은 감염과 약물 모두에 연결되어Ruby on Rails : 다형 다 대다 디자인?

create_table "diseases" do |t| 
    t.string  "name" 
end 

create_table "drugs" do |t| 
    t.string  "name" 
end 

create_table "symptoms" do |t| 
    t.string  "name" 
end 

create_table "to_symptoms" do |t| 
    t.integer "symptom_id" 
    t.integer "symptomatic_id" 
    t.string  "symptomatic_type" 
end 

. 까다로운 부분은 증상과 약물의 관계가 부작용이거나 금기 사항 일 수 있다는 것입니다. 방법은 내가 할 노력이 있었다 :

class ToSymptom < ActiveRecord::Base 
    belongs_to :symptomatic, :polymorphic => true  
    belongs_to :symptom 
end 

class Drug < ActiveRecord::Base 
    has_many :to_symptom, :as => :symptomatic 

    has_many :contraindications, :class_name => "Symptom", 
      :through => :to_symptom, :source => :symptomatic, 
      :source_type => 'Contraindication' 
    has_many :side_effects, :class_name => "Symptom", 
      :through => :to_symptom, :source => :symptomatic, 
      :source_type => 'SideEffect' 
end 

class Symptom < ActiveRecord::Base 
    has_many :to_symptom 

    has_many :diseases, :through => :to_symptom, :source => :symptomatic, 
      :source_type => 'Disease' 
    has_many :contraindicated_drugs, :class_name => "Drug", 
      :through => :to_symptom, :source => :symptomatic, 
      :source_type => 'Contraindication' 
    has_many :caused_by, :class_name => "Drug", :through => :to_symptom, 
      :source => :symptomatic, :source_type => 'SideEffect' 
end 

class Disease < ActiveRecord::Base 
    has_many :to_symptom, :as => :symptomatic 
    has_many :symptoms, :through => :to_symptom 
end 

질병 < -> 증상의 관계는 내가 기대했던 방식으로 작동하는 것 같군,하지만 약물과 증상 사이의 관계는 내가 기대했던 일을하지 않습니다 . 증상 -> 마약의 방향에있는 관계는 작동하는 것처럼 보이지만, 반대 방향으로 약간의 이상한 SQL이 생성됩니다. 내가 좋아하는 뭔가를 시도하는 경우 :

d = Drug.first 
d.contraindications 

을 거 야 다음 SQL 얻을 :

SELECT 
    `symptoms`.* 
FROM `symptoms` 
INNER JOIN `to_symptoms` ON `symptoms`.`id` = `to_symptoms`.`symptomatic_id` 
WHERE `to_symptoms`.`symptomatic_id` = 2 
    AND `to_symptoms`.`symptomatic_type` = 'Drug' 
    AND `to_symptoms`.`symptomatic_type` = 'Contraindication' 

to.symptoms.symptomatic_type = drug이 거기에 안하고는 to_symptoms의 잘못된 필드에 가입 (symptomatic_id 대를 symptom_id. 다양한 조합을 시도했지만,이 기능을 사용할 수없는 것 같습니다. RoR에서도 가능한 일을하려고합니까?

답변

0

이것이 아닌 것처럼 보입니다. 매우 널리 광고하지만 레일스에서는 작동하지 않는 것 같습니다. ... (polymorphic mas_many : through) (적어도 미친 해킹이없는 경우). 어떤 지원 링크를 찾으려고 노력할 것입니다.

+0

흠, 그게 내가 온라인으로 일하는 어떤 예도 찾을 수 없었던 이유를 설명해줍니다. 'contraindication '} Symptom.joins (: to_symptom) .where (: tosymptom => {: symptomatic_id => self.id, : symptomatic_type =>'금기증)}) end ' 그러나 그러한 연계를 구축하는 더 빠른 방법을 갖는 것이 여전히 좋을 것입니다. << 연산자에 과부하를주는 어떤 힌트를 주면 d.contraindications << Symptom.create (...)'와 같이 작동할까요? 아니면 그 기회를 갖기 위해 수업 금기를 만들어야합니까? 다시 한 번 감사드립니다! – Austin

+0

연산자 오버로드 : http : //strugglingwithruby.blogspot.com/2010/04/operator-overloading.html 페이지 하단에는 "<<"에 대한 예제가 나와 있습니다. 나는 그것이 당신의 문제를 어떻게 도울지를 완전히 따르지는 않습니다. 지금까지이 문제에 대한 좋은 해결책을 찾지 못했습니다. – noli