2011-03-17 115 views
0

我簡化了我的設計,使問題更加清晰。 (以下模型的設計)Rails關係幫助

我想要做的是從課程註冊中獲取所有PatientCourseSteps僅用於註冊(註冊由患者和課程組成)。

這是我曾嘗試:

#Gives me ALL the patient course steps regardless of the course 
course_enrollment.patient.patient_course_steps 

#Gives me ALL the patient course steps regardless of the patient 
course_enrollment.course.patient_course_steps 

下面是有必要的車型

class CourseEnrollment < ActiveRecord::Base 
    belongs_to :patient 
    belongs_to :course 
end 

class Course < ActiveRecord::Base 
    has_many :course_steps, :dependent => :destroy 
    has_many :steps, :through => :course_steps 
    has_many :course_enrollments, :dependent => :destroy 
    has_many :patients, :through =>:course_enrollments 
    has_many :patient_course_steps, :dependent => :destroy 
end 

class Patient < ActiveRecord::Base 
    belongs_to :user, :dependent => :destroy 
    has_many :enrollments, :dependent => :destroy 
    has_many :clients, :through => :enrollments 
    has_many :course_enrollments, :dependent => :destroy 
    has_many :courses, :through => :course_enrollments 
    has_many :patient_course_steps, :dependent => :destroy 
end 

class Step < ActiveRecord::Base 
    belongs_to :step_type 
    belongs_to :client 
    has_one :step_quiz, :dependent => :destroy 
    has_one :step_survey, :dependent => :destroy 
    has_one :step_text, :dependent => :destroy 
    has_one :step_download, :dependent => :destroy 
    has_one :step_video, :dependent => :destroy 
    has_one :step_presentation, :dependent => :destroy 
    has_many :course_steps, :dependent => :destroy 
    has_many :courses, :through => :course_steps 
    has_many :patient_course_steps, :dependent => :destroy 
end 



class PatientCourseStep < ActiveRecord::Base 
    belongs_to :patient 
    belongs_to :course 
    belongs_to :step 
end 

回答

0

我最終什麼事做了添加一個方法來CourseEnrollment命名patient_course_steps這對於哪些查詢我需要。

def patient_course_steps 
    PatientCourseStep.where(:patient_id => self.patient_id, :course_id => self.course_id) 
end