title: How can I order by attributes of multiple subclasses that inherit from the same base class in Ruby on Rails?
body: <p>I have 4 classes - Patient, Doctor, Person, and Appointment.  Patient and Doctor are subclasses of Person.  Appointment belongs to Patient and belongs to Doctor.</p>
  
  <p>I want to write an AR statement to sort appointments by the patient's last name and another to sort by the doctor's last name, as all appointments will be in a sortable table.  I am a bit confused as to what to put in the "order" option of the AR find statement.  If I put</p>
  
  <pre><code>:order =&gt; 'patient.last_name'
  </code></pre>
  
  <p>I get a mysql error - "Unknown column 'patient.last_name' 
  Which makes sense because there is no patient column, it is a patient_id referring to a foreign "person" object.  Of course I can sort by person.last_name but I am not sure how to specify which type of person to sort by - doctor or patient.</p>
  
  <p>I should also note that I am using the include option to eager load the patient and doctor.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>There is only a person table and a appointments table.  The patient and doctor inherit from the person.  patients.last_name will not work because there is no patients table.</p>
  
  <p>The AR statement is:</p>
  
  <pre><code>find  :all,
                  :include =&gt; [:doctor, :patient], 
                  :order =&gt; 'something needs to go here'   
  </code></pre>
  
  <p>The 'something needs to go here' should be a statement to order by either the doctor's last name or the patient's last name.</p>