我有两个需要一起分页的范围查询。单独地,它们看起来像这样:
a = @person.company.tasks.open.paginate :page => params[:page], :order => (sort_column + " " + sort_direction)
b = @person.tasks.private.paginate :page => params[:page], :order => (sort_column + " " + sort_direction)
我想做以下事情:
@tasks = (a + b).paginate :page => params[:page], :order => (sort_column + " " + sort_direction)
当我执行@tasks = (a + b)
并且结果分页时,我成功地获得了合并。
将变量添加到一起后,排序将不再起作用。
答案 0 :(得分:1)
您可以进行一次查询,根据2个查询返回的ID获取任务:
company_task_ids = @person.company.tasks.open.pluck(:id)
private_task_ids = @person.tasks.private.pluck(:id)
@tasks = Task.where(:id => (company_task_ids + private_task_ids)).paginate :page => params[:page], :order => (sort_column + " " + sort_direction)
这可以稍微优化一下,但这应该可以帮助你