{"action":"created","issue":{"url":"https://api.github.com/repos/ga-dc/pbj-project4/issues/24","labels_url":"https://api.github.com/repos/ga-dc/pbj-project4/issues/24/labels{/name}","comments_url":"https://api.github.com/repos/ga-dc/pbj-project4/issues/24/comments","events_url":"https://api.github.com/repos/ga-dc/pbj-project4/issues/24/events","html_url":"https://github.com/ga-dc/pbj-project4/issues/24","id":106770404,"number":24,"title":"Rails resources, join tables, how to query?","user":{"login":"tomBeach","id":10157094,"avatar_url":"https://avatars.githubusercontent.com/u/10157094?v=3","gravatar_id":"","url":"https://api.github.com/users/tomBeach","html_url":"https://github.com/tomBeach","followers_url":"https://api.github.com/users/tomBeach/followers","following_url":"https://api.github.com/users/tomBeach/following{/other_user}","gists_url":"https://api.github.com/users/tomBeach/gists{/gist_id}","starred_url":"https://api.github.com/users/tomBeach/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomBeach/subscriptions","organizations_url":"https://api.github.com/users/tomBeach/orgs","repos_url":"https://api.github.com/users/tomBeach/repos","events_url":"https://api.github.com/users/tomBeach/events{/privacy}","received_events_url":"https://api.github.com/users/tomBeach/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-09-16T13:10:43Z","updated_at":"2015-09-16T13:27:21Z","closed_at":null,"body":"I've tried five ways to query tables on either side of a join table but still can't seem to get rails magic happening.  It may be that my relationships are not correct.  The attached ERD is the design I am trying to use (no events or appointments tables yet).  I forgot to include the assignment table, which is a join between Person and Patient.  A patient will always be a person, but not the other way around.  (A person can be a doctor, nurse, etc.  Note that I am using two person ids in the patient table -- one to link to the patient's person data, and one to link to the patient's doctor's data.  The links happen via the appointments table -- not shown.  Is this asking for trouble?)\r\n![erd](https://cloud.githubusercontent.com/assets/10157094/9905133/0aa25e74-5c51-11e5-870b-e746bb444d2f.jpg)\r\nHere are my models:\r\n```\r\nclass Person < ActiveRecord::Base\r\n    has_many :assignments\r\n    has_many :patients, through: :assignments\r\nend\r\nclass Patient < ActiveRecord::Base\r\n    has_many :assignments\r\nend\r\nclass Assignment < ActiveRecord::Base\r\n  belongs_to :person\r\n  belongs_to :patient\r\nend\r\n```\r\nHere is the controller:\r\n```\r\n    def index\r\n        puts \"******* WelcomeController.index ******* \"\r\n        @persons = Person.all\r\n    end\r\n```\r\nHere is code from the generic view I am using to test query results.  It works but I try to use person.patient.room (using rails magic to link person and patient via the assignments join) it can't find \"room\".  \r\n```\r\n<h2>Patient Rooms</h2>\r\n\r\n<div class=\"patients\">\r\n    <ul>\r\n        <% @persons.each do |person| %>\r\n            <% @patients = person.patients %>\r\n            <% @patients.each do |patient| %>\r\n                  <p><h1><%= patient.room %><%= patient.bed %><a href=\"/persons/<%= person.id %>\"></h1><%= person.first_name %> <%= person.last_name %></a></p>\r\n            <% end %>\r\n        <% end %>\r\n    </ul>\r\n</div>\r\n```\r\nBasically I want to know if the models accurately represent the relationships I am trying to use...  Thanks!"},"comment":{"url":"https://api.github.com/repos/ga-dc/pbj-project4/issues/comments/140741872","html_url":"https://github.com/ga-dc/pbj-project4/issues/24#issuecomment-140741872","issue_url":"https://api.github.com/repos/ga-dc/pbj-project4/issues/24","id":140741872,"user":{"login":"tomBeach","id":10157094,"avatar_url":"https://avatars.githubusercontent.com/u/10157094?v=3","gravatar_id":"","url":"https://api.github.com/users/tomBeach","html_url":"https://github.com/tomBeach","followers_url":"https://api.github.com/users/tomBeach/followers","following_url":"https://api.github.com/users/tomBeach/following{/other_user}","gists_url":"https://api.github.com/users/tomBeach/gists{/gist_id}","starred_url":"https://api.github.com/users/tomBeach/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomBeach/subscriptions","organizations_url":"https://api.github.com/users/tomBeach/orgs","repos_url":"https://api.github.com/users/tomBeach/repos","events_url":"https://api.github.com/users/tomBeach/events{/privacy}","received_events_url":"https://api.github.com/users/tomBeach/received_events","type":"User","site_admin":false},"created_at":"2015-09-16T13:27:21Z","updated_at":"2015-09-16T13:27:21Z","body":"Here is the code I was hoping to use (below).  It produces this error:\r\n```\r\nActiveRecord::RecordNotFound in WelcomeController#index\r\nCouldn't find Patient with 'id'=3\r\n```\r\nThe reason it can't find patient with id=3 is that id=3 is a doctor.  3 is the id in the person table.  This implies the person.category = \"patient\" line is not working.  (I'm looking only for patients but need info from the persons table.)  Should I be using Assignment.fin instead of Patient.find?  \r\n```\r\n <% @patient = Person.where(person.id, person.category = \"patient\") %>\r\n      <% if @patient %>\r\n          <% @patient_info = Patient.find(person.id) %>\r\n          <p><h1><%= @patient_info.room %><%= @patient_info.bed %><a href=\"/persons/<%= person.id %>\"></h1>\r\n              <%= person.first_name %> <%= person.last_name %></a></p>\r\n      <% end %>\r\n```"}}