如果我已经在使用 Pagy,将 pagy_search 集成到我的索引中的最佳方法是什么?在我的控制器中,我目前有:
def index
@gold_drivers = Driver.gold_plan
@silver_drivers = Driver.silver_plan
@free_drivers = Driver.free_plan
@pagy, @drivers = pagy_array(
@gold_drivers + @silver_drivers + @free_drivers,
page: params[:page],
items: 16
)
if params[:query].present?
@drivers = @gold_drivers + @silver_drivers + @free_drivers
drivers_search = Driver.pagy_search(params[:query])
@pagy, @drivers = pagy_meilisearch(drivers_search, items: 25, page: params[:page])
end
end
我有一个 Driver 模型,它委托给 Profile 模型。不幸的是,当我点击搜索时,我得到
Driver#first_name delegated to profile.first_name, but profile is nil: #<Driver id: 224...
据我所知,该驱动程序没有配置文件,但运行 @gold_drivers = Driver.gold_plan / @silver_drivers = Driver.silver_plan 等的查询不应选择没有配置文件的驱动程序。
Driver.gold_plan 后面的查询是:
find_by_sql("SELECT drivers.*, (profiles.no_races + profiles.no_poles + profiles.no_podiums + profiles.no_wins) AS scores FROM drivers INNER JOIN profiles ON profiles.driver_id = drivers.id INNER JOIN subscriptions ON subscriptions.driver_id = drivers.id WHERE subscriptions.status = 'active' AND profiles.first_name != null OR profiles.first_name != '' AND subscriptions.stripe_plan = '#{ENV["GOLD_ANNUAL"]}' OR subscriptions.stripe_plan = '#{ENV["GOLD_MONTH"]}' GROUP BY scores, drivers.id ORDER BY scores DESC")
在那个查询中,我使用"AND profiles.first_name != null OR profiles.first_name != ''"
的是在 pagy_page 部分有效的,但在 pagy_search 部分无效。
对此的任何方向表示赞赏!