David Mitchell
2006-Feb-06 22:50 UTC
[Rails] Sorting records from multiple tables, on different fieldnames
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
|event|
try
# See if the datedue field exists in this record; rescue if not...
if event.datedue
...
which is *really* ugly?
Thanks in advance
Dave M.
