Rails Relationship Помощь - PullRequest
       1

Rails Relationship Помощь

0 голосов
/ 17 марта 2011

Я упростила свой дизайн, чтобы прояснить этот вопрос. (Дизайн моделей ниже)

Что я пытаюсь сделать, так это из CourseEnrollment получить все 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

1 Ответ

0 голосов
/ 17 марта 2011

В итоге я добавил метод CourseEnrollment с именем Patient_Course_steps, который запрашивает то, что мне нужно.

def patient_course_steps
  PatientCourseStep.where(:patient_id => self.patient_id, :course_id => self.course_id)
end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...