如何使用 ransack 查询链接作用域

How to chain scopes with ransack queries

一位用户提交了搜索查询以查找 employers。我想先使用 scope 过滤掉 employers table 中的记录,然后我想 运行 对那个 activerecord::relation 对象进行搜查查询return 从那个 scope 编辑而来。

特别是按照这个顺序。重要的是 scope 首先是 运行,因为它 return 只有 select 条记录,然后 ransack 查询可以进一步过滤这些记录。在我的情况下,运行先进行搜查查询,然后 运行在 return 运行 上进行 scope 查询非常慢,因为搜查查询可能 return很多很多记录。

这听起来像是将查询链接在一起的工作。我知道如何将常规范围链接在一起:

# example
# `active`, `from_department_stores`, and `small_company` are all scopes
@employers = Employer.active.from_department_stores.small_company 

但我不知道如何将 ransack 查询链接到范围,并以范围首先是 运行 的方式进行。

代码:

@q = Employer.ransack(params[:q])
# Pretty sure this will not work, but hopefully it provides a good idea
# of what I am trying to do
@employers = Employer.active.@q.result

Ransack Documentation

这适用于 you.Thanks

@employers = Employer.active.ransack(params[:q]).result