2011-03-17 103 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