使用has_and_belongs_to_many获取关联对象轨道的列表 [英] Using has_and_belongs_to_many to get list of associated objects rails

查看:67
本文介绍了使用has_and_belongs_to_many获取关联对象轨道的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,ClinicianPatient. clinician has_many: patientspatient belongs_to :clinician.第三个模型SharedPatient用于存储patientsclinicians之间的其他关联,因为patient除了belongs_to之外还可以由许多其他clinicians共享.这是通过has_and_belongs_to_many关系完成的.

I have two models, Clinician and Patient. A clinician has_many: patients and a patient belongs_to :clinician. A third model, SharedPatient is meant to store additional assosiactions between patients and clinicians as a patient can be shared by many other clinicians besides the one it belongs_to. This is done using a has_and_belongs_to_many relationship.

查看型号:

class Clinician < ActiveRecord::Base
 has_many :patients
 has_and_belongs_to_many :shared_patients, join_table: 'shared_patients'
end

class Patient < ActiveRecord::Base
 belongs_to :clinician
 has_and_belongs_to_many :shared_clinicians, join_table: 'shared_patients'
end

class SharedPatient < ActiveRecord::Base
 belongs_to :clinician
 belongs_to :patient
end

这是在数据库中设置我的模型的方式:

This is how my models are set out in the db:

Clinician:
 first_name: string
 last_name: string
 user_id: integer

Patient:
 clinician_id: integer
 first_name: string
 last_name: string
 user_id: integer

SharedPatient:
 clinician_id: integer
 patient_id: integer

使用这些,我想显示与patient共享的clinicians的列表.

Using these I would like to show the list of clinicians that a patient is shared with.

这就是我现在在控制器中所拥有的:

This is what I have in my controller now:

@patient = Patient.find_by(user_id: current_user.patient.id)
@clinicianslist = @patient.shared_clinicians

它正尝试使用以下视图在视图中显示这些内容:

It am trying to show these in a view using:

<% @clinicianslist.each do |list| %>
 <p><%= link_to Clinician.find_by(id: list).full_name, clinician_path(list) %></p>
<% end %>

使用我现在拥有的内容,尝试加载视图时出现错误:

Using what I have now I am getting an error when trying to load the view:

患者中的

NameError#show

NameError in Patients#show

未初始化的固定患者:: SharedClinician

uninitialized constant Patient::SharedClinician

我在运行时遇到相同的错误

I get the same error when running

Patient.find_by(id:1259).shared_clinicians

Patient.find_by(id: 1259).shared_clinicians

在控制台中.

关于解决此错误或构建模型以获取我想要的关联的任何建议都很好.谢谢

Any advice on solving this error or on structuring the models to get the associations I want would be great. Thanks

推荐答案

您不需要SharedPatient模型,因此应将其删除.

You don't need the SharedPatient model, so this should be deleted.

错误是因为Rails不能从关联名称或表名称中猜测类名称.试试这个:

The error is because Rails cannot guess the class name from the association name or the table name. Try this:

class Clinician < ActiveRecord::Base
 has_many :patients
 has_and_belongs_to_many :shared_patients, join_table: 'shared_patients', class_name: 'Patient'
end

class Patient < ActiveRecord::Base
 belongs_to :clinician
 has_and_belongs_to_many :shared_clinicians, join_table: 'shared_patients', class_name: 'Clinician'
end

这告诉Rails使用哪个类.当前,正在猜测SharedClinician,这是错误的.

This tells Rails which class to use. Currently, it is guessing SharedClinician, which is wrong.

这篇关于使用has_and_belongs_to_many获取关联对象轨道的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆