从数组值中调用其他模型的对象rails
Call up other model's objects from array values rails
我有两个模型,Clinician
和 Patient
。一个 clinician
has_many: patients
和一个 patient
belongs_to :clinician
.
除了临床医生说一个病人 belongs_to
一个病人还有一个名为 shared_with
的列,它是一个包含 clinician.id
数组的字符串。这是使用 serialize :shared_with, Array
.
完成的
我希望能够 select 从临床医生 full_name
的下拉列表中仅针对 id
包含在 shared_with
数组中的临床医生。
<%= form_for [@clinician, @comment] do |form| %>
<div class="form-group">
<%= form.label :clinician_id %>
<%= form.collection_select :clinician_id, Clinician.all.order("last_name asc"), :id, :full_name, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :general_comment %>
<%= form.text_area :general_comment, class: "form-control", rows: 5, placeholder: "Leave a comment" %>
</div>
<%= form.button 'Submit Comment', class: "btn btn-u btn-success" %>
我目前有 Clinician.all.order("last_name asc")
我想对其进行排序,以便我只有这个较短的列表。
我认为这将类似于用 @clinicians
替换我现在拥有的并将其定义为:
@patient = Patient.find_by(user_id: current_user.patient.id).shared_with
@clinicians = a list of clinicians where id: @patient.each
并使用一些能够为我做到这一点的方法。
如有任何建议,我们将不胜感激。谢谢
which is a string holding an array of clinician.id
这太可怕了!不要以这种方式存储 ID。您正在尝试用您的问题解决错误的问题。
您的 shared_with id 数组在数据库中应该是多对多关系,它自己的 table(Rails 称之为 has_and_belongs_to_many
)。一个病人可以与许多临床医生共享,一个临床医生可以共享许多病人。在继续之前阅读 Rails 关于多对多关系的指南:http://guides.rubyonrails.org/association_basics.html#has-and-belongs-to-many-association-reference
您最终应该能够致电 @patient.shared_clinicians
或类似电话以获取下拉列表中的临床医生列表。
您的模型看起来像这样:
class Patient < ActiveRecord::Base
belongs_to :clinician
has_and_belongs_to_many :shared_clinicians, join_table: 'shared_patients'
end
class Clinician < ActiveRecord::Base
has_many :patients
has_and_belongs_to_many :shared_patients, join_table: 'shared_patients'
end
然后数据库 table shared_patients
有两列:clinician_id
和 patient_id
我有两个模型,Clinician
和 Patient
。一个 clinician
has_many: patients
和一个 patient
belongs_to :clinician
.
除了临床医生说一个病人 belongs_to
一个病人还有一个名为 shared_with
的列,它是一个包含 clinician.id
数组的字符串。这是使用 serialize :shared_with, Array
.
我希望能够 select 从临床医生 full_name
的下拉列表中仅针对 id
包含在 shared_with
数组中的临床医生。
<%= form_for [@clinician, @comment] do |form| %>
<div class="form-group">
<%= form.label :clinician_id %>
<%= form.collection_select :clinician_id, Clinician.all.order("last_name asc"), :id, :full_name, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :general_comment %>
<%= form.text_area :general_comment, class: "form-control", rows: 5, placeholder: "Leave a comment" %>
</div>
<%= form.button 'Submit Comment', class: "btn btn-u btn-success" %>
我目前有 Clinician.all.order("last_name asc")
我想对其进行排序,以便我只有这个较短的列表。
我认为这将类似于用 @clinicians
替换我现在拥有的并将其定义为:
@patient = Patient.find_by(user_id: current_user.patient.id).shared_with
@clinicians = a list of clinicians where id: @patient.each
并使用一些能够为我做到这一点的方法。
如有任何建议,我们将不胜感激。谢谢
which is a string holding an array of
clinician.id
这太可怕了!不要以这种方式存储 ID。您正在尝试用您的问题解决错误的问题。
您的 shared_with id 数组在数据库中应该是多对多关系,它自己的 table(Rails 称之为 has_and_belongs_to_many
)。一个病人可以与许多临床医生共享,一个临床医生可以共享许多病人。在继续之前阅读 Rails 关于多对多关系的指南:http://guides.rubyonrails.org/association_basics.html#has-and-belongs-to-many-association-reference
您最终应该能够致电 @patient.shared_clinicians
或类似电话以获取下拉列表中的临床医生列表。
您的模型看起来像这样:
class Patient < ActiveRecord::Base
belongs_to :clinician
has_and_belongs_to_many :shared_clinicians, join_table: 'shared_patients'
end
class Clinician < ActiveRecord::Base
has_many :patients
has_and_belongs_to_many :shared_patients, join_table: 'shared_patients'
end
然后数据库 table shared_patients
有两列:clinician_id
和 patient_id