0

I have two models, Clinician and Patient. A clinician has_many: patients and a patient belongs_to :clinician.

Besides the clinician that a patient belongs_to a patient also has a column called shared_with, which is a string holding an array of clinician.id. This is done using serialize :shared_with, Array.

I would like to be able to select from a drop down of clinician's full_name for only clinicians who's id is included in the shared_with array.

<%= 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" %>

Where I currently have Clinician.all.order("last_name asc") I would like to sort it so that I only have this shorter list.

I think it will be something like replacing what I have now with @clinicians and defining that as:

@patient = Patient.find_by(user_id: current_user.patient.id).shared_with
@clinicians = a list of clinicians where id: @patient.each

And using some method that is able to do this for me.

Any advice would be appreciated. Thanks

Skiapex
  • 153
  • 3
  • 14

1 Answers1

1

which is a string holding an array of clinician.id

This terrible! Do not store the ids this way. You are trying to solve the wrong problem with your question.

Your array of shared_with ids should be a many to many relationship in the database with it's own table (Rails calls this has_and_belongs_to_many). A patient can be shared with many clinicians and a clinician may share many patients. Read the Rails guide on many to many relationships before going further: http://guides.rubyonrails.org/association_basics.html#has-and-belongs-to-many-association-reference

You should end up being able to call @patient.shared_clinicians or similar to get a list of the clinicians for the dropdown.

Your models would look something like this:

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

Then a database table shared_patients with two columns: clinician_id and patient_id

Matt Gibson
  • 14,616
  • 7
  • 47
  • 79
  • Thanks @Matt, you're definitely right that using a table is a better way to manage these relationships. Still learning Rails... If I do as you recommend and create a `SharedPatient` table and the `has_and_belongs_to_many` relationships I still get an error. I've seeded the database with a couple of examples but when I run `Patient.find_by(id: 1259).shared_patients.first` or `Patient.find_by(id: 1259).shared_clinicians.first` I get `NoMethodError: undefined method `shared_patients' for #` or `uninitialized constant Patient::SharedClinician`. Advice? – Skiapex May 25 '15 at 23:44
  • For `SharedPatient` I have added `belongs_to :clinician` and `belongs_to :patient` – Skiapex May 25 '15 at 23:45
  • `NoMethodError: undefined method shared_patients' for #` looks like the wrong method being called. Does `Patient.find_by(id: 1259).shared_clinicians` not work? – Matt Gibson May 25 '15 at 23:47
  • I get back `NameError: uninitialized constant Patient::SharedClinician`. There is no table `SharedClinician` – Skiapex May 25 '15 at 23:49
  • Hmmm. It might be easier if you make a new question where you post all the code for your models, along with your db migration files. It's tricky to follow in the comments. – Matt Gibson May 26 '15 at 09:21
  • Please see my [new question](http://stackoverflow.com/questions/30462516/using-has-and-belongs-to-many-to-get-list-of-associated-objects-rails) – Skiapex May 26 '15 at 15:15