I'm wondering if there's an efficient way to combine the results of multiple ActiveRecord objects in Rails. For example, I might make three individual calls to three individual tables, and I want the results combined, and sorted by a common column.
Here's a super basic code example that will hopefully make my question easier to understand:
@results1 = Table1.find(:all)
@results2 = Table2.find(:all)
@results3 = Table3.find(:all)
@combined_results_sorted_by_date_column = (how?)
As suggested by others, here's one solution to the problem.
@combined_results = @result1 + @result2 + @result3
@combined_results.sort! {|x,y| x.date <=> y.date}
What if I want to sort by date, but Table3 refers to the "created_on" column as date?