【发布时间】:2015-12-19 00:06:22
【问题描述】:
我想不通的简单问题。
如何获得与另一类项目列表相关联的关联项目列表。例如Clinicianhas_manypatients 到care_group_assignments 和patientshas_manyassessments。我想要一个patients 的列表给我他们所有assessments 的列表。 @patients 给了我patients 的列表;我如何获得他们的assessments 列表?我希望@assessments 成为与给定clinician 关联的所有patients 关联的所有assessments 的列表。
我有一个 has_many :through 'Clinician' 和 'Patient' 之间的关系以及一个连接模型 'CareGroupAssignment'。
clinician.rb(简体)
class Clinician < ActiveRecord::Base
has_many :patients ,:through=> :care_group_assignments
has_many :care_group_assignments, :dependent => :destroy
has_many :assessments, :through => :patients
belongs_to :user
accepts_nested_attributes_for :user, :allow_destroy => true
end
患者.rb
class Patient < ActiveRecord::Base
has_many :clinicians ,:through=> :care_group_assignments
has_many :care_group_assignments
has_many :assessments, dependent: :destroy
belongs_to :user
accepts_nested_attributes_for :user, :allow_destroy => true
end
care_group_assignments.rb
class CareGroupAssignment < ActiveRecord::Base
belongs_to :clinician
belongs_to :patient
end
我尝试使用来自4720492、9408931 和15256541 的答案。
我正在尝试获取 ClinicianController 中的列表:
def show
@clinician = Clinician.find_by(id: params["id"])
@patients = @clinician.patients
@assessments = @patients.assessments.order("created_at desc")
end
current_clinician 在我的ApplicationController 中定义为:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user
before_action :require_user
helper_method :current_clinician
before_action :require_clinician
def current_user
@current_user ||= User.find_by!(auth_token: cookies[:auth_token]) if cookies[:auth_token]
end
def require_user
if current_user.nil?
redirect_to new_session_path
end
end
def current_clinician
current_user.clinician
end
def require_clinician
if current_clinician.nil?
redirect_to new_session_path
end
end
end
现在我收到了NoMethodError in CliniciansController#show,
#Patient 的未定义方法“评估”:.....
Rails 4.1.8,红宝石 2.2.1p85,PostgreSQL
谢谢
【问题讨论】:
-
你能描述一下你想在
@esas_assessments = @patients.assessments.order("created_at desc")中显示什么吗? -
我想要一个
patients的列表来给我他们所有assessments的列表。@patients给了我病人名单;我如何获得他们的assessments列表?我希望@assessments成为与给定clinician关联的所有patients关联的所有assessments的列表。
标签: ruby-on-rails nested has-many-through nomethoderror