从 has_many 获取列表关联 :through with a nested has_many in rails
Getting the list associations from has_many :through with a nested has_many in rails
简单的问题我想不通。
如何获取与另一类型项目列表关联的关联项目列表。例如 Clinician
has_many
patients
到 care_group_assignments
和 patients
has_many
assessments
。我想要一个 patients
的列表给我一个他们所有 assessments
的列表。 @patients
给我 patients
的列表;我如何获得他们的 assessments
列表?我希望 @assessments
成为与给定 clinician
.
关联的所有 patients
关联的所有 assessments
的列表
我在 'Clinician' 和 'Patient' 之间有一个 has_many :through 关系,并且有一个联合模型 '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
patient.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, and 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
,
undefined method `assessments' for #Patient:.....
Rails 4.1.8,ruby 2.2.1p85,PostgreSQL
谢谢
根据您的 Patient
模型,Patient
has_many
评估,但实际上 show
您使用:
@assessments = @patients.assessments.order("created_at desc")
其中 @patients
- 模型的项目列表 Patient
。
你应该在那里重构你的逻辑。
尝试这样做:
@assessments = Assessment.joins(:patient => {:care_group_assignments => :clinician).where(["clinicians.id = ?", @clinician.id]).order("assessments.created_at desc")
希望对你有所帮助。
你试过@clinician.assessments
了吗?通过 AR 和 join tables 的魔力,你应该能够得到你想要的结果!
简单的问题我想不通。
如何获取与另一类型项目列表关联的关联项目列表。例如 Clinician
has_many
patients
到 care_group_assignments
和 patients
has_many
assessments
。我想要一个 patients
的列表给我一个他们所有 assessments
的列表。 @patients
给我 patients
的列表;我如何获得他们的 assessments
列表?我希望 @assessments
成为与给定 clinician
.
patients
关联的所有 assessments
的列表
我在 'Clinician' 和 'Patient' 之间有一个 has_many :through 关系,并且有一个联合模型 '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
patient.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, and 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
,
undefined method `assessments' for #Patient:.....
Rails 4.1.8,ruby 2.2.1p85,PostgreSQL
谢谢
根据您的 Patient
模型,Patient
has_many
评估,但实际上 show
您使用:
@assessments = @patients.assessments.order("created_at desc")
其中 @patients
- 模型的项目列表 Patient
。
你应该在那里重构你的逻辑。
尝试这样做:
@assessments = Assessment.joins(:patient => {:care_group_assignments => :clinician).where(["clinicians.id = ?", @clinician.id]).order("assessments.created_at desc")
希望对你有所帮助。
你试过@clinician.assessments
了吗?通过 AR 和 join tables 的魔力,你应该能够得到你想要的结果!