Simple problem I can't figure out.
How do you get a list of associated items that are associated with a list of another type of item. For example a Clinician
has_many
patients
through care_group_assignments
and patients
has_many
assessments
. I want a list of patients
to give me a list of all of their assessments
. @patients
gives me the list of patients
; how do I get their lists of assessments
? I want @assessments
to be a list of all of the assessments
associated with all of the patients
associated with a given clinician
.
I have a has_many :through relationship between 'Clinician', and 'Patient' with a joined model 'CareGroupAssignment'.
clinician.rb (simplified)
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
I tried using answers from 4720492, 9408931, and 15256541.
I am trying to get the list in the ClinicianController:
def show
@clinician = Clinician.find_by(id: params["id"])
@patients = @clinician.patients
@assessments = @patients.assessments.order("created_at desc")
end
current_clinician
is defined in my ApplicationController
as:
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
Right now I get a NoMethodError in CliniciansController#show
,
undefined method `assessments' for #Patient:.....
Rails 4.1.8, ruby 2.2.1p85, PostgreSQL
Thanks