I've banged my head on this for 2 days, and need to step back and ask for help.
I'm doing what I would think would be a common occurrence - viewing a
list of records sorted by the latest item in a has_many relationship.
Contacts have many appointments, with one next_appointment that is the
My view lists the contacts, using pagination. One of the displayed
columns is the next_appointment. I want to support sorting the list
on the next_appointment. Problem is I always get *every* appointment
for each contact in the list. I can't seem to weed everything except
the latest appointment.
Doesn't matter if I use the :include or not, and DISTINCT doesn't work
becuase the records are real duplicates. I haven't tried GROUP BY yet
because I don't think it's going to weed anything out, though I may be
wrong about that.
I have the following relationships:
class Contact < ActiveRecord::Base
has_many :appointment, :dependent => :destroy, :order =>
has_one :next_appointment, :class_name => 'Appointment',
:order => 'appointment_on DESC'
class Appointment < ActiveRecord::Base
Does anybody know a way to handle this situation?