Just started learning active record and am wondering how to best retrieve data from multiple tables where an SQL aggregate query is involved.
In the following example (f
As Pallan pointed out, the :select
option cannot be used with the :include
option. However the :joins
option can. And this is what you want here. In fact, it can take the same arguments as :include
or use your own SQL. Here's some rough, untested code, may need some minor fiddling.
Event.all(:select => "events.id, patients.lname, events.patient_id, events.event_type, max(events.event_date) as max_date", :joins => :patient, :group => "patients.lname, events.patient_id, events.event_type")
Note I modified things slightly. I renamed the event_date
alias to max_date
so there's no confusion over which attribute you are referring to. The attributes used in your :select
query are available in the models returned. For example, in this you can call event.max_date
. I also added the event id
column because you can sometimes get some nasty errors without an id
attribute (depending on how you use the returned models).
The primary difference between :include
and :joins
is that the former performs eager loading of the associated models. In other words, it will automatically fetch the associated patient object for each event. This requires control of the select
statement because it needs to select the patient attributes at the same time. With :joins
the patient objects are not instantiated.