Forum: Ruby on Rails Sorting records from multiple tables, on different fieldname

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
David M. (Guest)
on 2006-02-07 00:51
(Received via mailing list)
Hello list,

I've got 3 tables containing what could loosely be called "events"
(but with totally different characteristics, which is why they're in
different tables), and I want to present all the records from all 3
tables in date order in an "Upcoming events" view.  To make it more
challenging, there's multiple date fields in each of the 3 tables, so
I need to sort on the field "datecommitted" from the first table,
"datedue" from the 2nd table, and "reviewdate" from the 3rd table.

This is what's known as an "unforeseen requirement", otherwise known
as the "Actually, you know what *would* be great, and we *should* have
requested this up front..." request.  Grrr!

Is there a reasonably elegant Rails-ish or Ruby-ish way to do this
(bearing in mind the different date fieldnames in each table), or
should I resort to something like:

@eventsA = A.find(:all)
@eventsB = B.find(:all)
@eventsC = C.find(:all)
@events_all = @eventsA.push(@eventsB).push(@eventsC)
@events_all.each do
    # See if the datedue field exists in this record; rescue if not...
    if event.datedue

which is *really* ugly?

Thanks in advance

Dave M.
This topic is locked and can not be replied to.