【问题标题】:Getting the list associations from has_many :through with a nested has_many in rails从 has_many 获取列表关联:通过在 rails 中使用嵌套的 has_many
【发布时间】:2015-12-19 00:06:22
【问题描述】:

我想不通的简单问题。

如何获得与另一类项目列表相关联的关联项目列表。例如Clinicianhas_manypatientscare_group_assignmentspatientshas_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

我尝试使用来自4720492940893115256541 的答案。

我正在尝试获取 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


【解决方案1】:

你试过@clinician.assessments吗?您应该能够通过 AR 和连接表的魔力获得您想要的结果!

【讨论】:

  • 成功了!谢谢。不敢相信就这么简单
  • 没问题!为了将来参考,当您拥有 has_many/belongs_to 方法时,它们将成为模型可调用方法的一部分。例如,对于任何临床医生,除了临床医生的属性外,您还可以调用以下方法:clinicalian.patients、clinicalian.care_group_assignments、clinicalian.assessments 和clinicalian.user。 Active Record 为您完成所有繁重的工作(生成 SQL 查询)!
【解决方案2】:

根据您的 Patient 模型,Patient has_many 评估,但在实际使用 show

@assessments = @patients.assessments.order("created_at desc")

其中@patients - 模型Patient的项目列表。

你应该在那里重构你的逻辑。

【讨论】:

  • 你建议我如何重构我的逻辑?我想要一个项目列表来给我一个他们所有子项目的列表。 @patients 给了我病人名单;我如何获得他们的assessments 列表?
  • 至少你应该使用连接,就像在下一个答案中一样。
【解决方案3】:

尝试这样做:

 @assessments = Assessment.joins(:patient => {:care_group_assignments => :clinician).where(["clinicians.id = ?", @clinician.id]).order("assessments.created_at desc")

希望对你有所帮助。

【讨论】:

  • 在 Pages#home 中返回 ActiveRecord::ConfigurationError Association named 'clinician' was not found on Patient; perhaps you misspelled it?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
相关资源
最近更新 更多