通过范围搜索

Ransack search through scope

我有一个模型 Task,其作用域名为 done。 一个Task属于一个Project和一个User.

我需要按用户已完成的项目筛选用户 task

simple_form_for @q do |form|
  form.input :tasks_done_project_id, collection: Project.ordered

但是使用“_done”不起作用。

我想到了一个劫掠者:

  ransacker :done do |parent|
    parent.done.table[:id]
  end

但是也不管用。有什么想法吗?

来自the documentation

By default, searching and sorting are authorized on any column of your model and no class methods/scopes are whitelisted.

...

# `ransackable_scopes` by default returns an empty array
# i.e. no class methods/scopes are authorized.
# For overriding with a whitelist array of *symbols*.
#
def ransackable_scopes(auth_object = nil)
  []
end

在您的 Task 模型中,您必须明确地将 done 范围列入白名单:

class Task < ActiveRecord::Base
  scope :done, -> { ... }

  def self.ransackable_scopes(auth_object = nil)
    [:done]
  end
end