0

我想显示组合来自多个查询的信息的数据。例如:

@projects = @company.projects
@assignments = @company.assignments
@tasks = @company.tasks

如何获取这三个查询并显示一个显示项目、分配和任务的大表,所有这些都按 排序created_at?所需的结果将如下所示:

7/11  Task created by company Xc for project Xp.
7/10  Project Xp created by User U.
7/09  Task created by company Yc for project Yp.
7/09  Assignment created by company Yc for project Yp.
7/08  Project Yp created by user U.
4

1 回答 1

2

UNION如果你想做更复杂的事情,比如只获取最新的 x 条目或进行分页,你可以使用 SQL查询。

如果您知道数据很少,您可以将结果收集到一个数组中并按created_at

@events = @projects.to_a + @assignments.to_a + @tasks.to_a
@events.sort_by! { |e| e.created_at }
于 2013-07-11T09:39:55.947 回答