ransack 范围内的参数数量错误(给定 0,预期 1)

wrong number of arguments (given 0, expected 1) in ransack scope

I was getting this error

控制器

def index
 @q = JobSubmission.ransack(search_params[:q])
 @job_submissions = @q.result(distinct: true)
 @job_submissions = @job_submissions.page(params[:page]).per(10)
end

我在作业和作业提交之间创建了关联

Job.rb 型号

class Job < ApplicationRecord
  has_many :job_submissions
end

JobSubmission.rb 型号

class JobSubmission < ApplicationRecord
  belongs_to :job
  scope :by_job, ->(id) { includes(:job).where(jobs:{id: id}) }

  def self.ransackable_scopes(auth_object = nil)
    %i(by_job)
  end
end

搜索表单

.search-panel
  %h4 Search File
  = search_form_for(@q, url:admin_job_submissions_path, method: :get, as: :q)  do |f|
    = f.label :by_job, "Job:"
    = f.collection_select(:by_job, Job.all, :id, :job_title, :include_blank => "None")

    = f.submit 'Search' , class: "button action-button__new-button"

我有 2 个工作和 4 个工作提交。

Job Collection

id: 1, job_title: "Job 1"
id: 2, job_title: "Job 2"

Job Submission Collection

id: 1, job_id: 1, file: "Job submission 1"
id: 2, job_id: 2, file: "Job submission 2"
id: 3, job_id: 1, file: "Job submission test to 1"
id: 4, job_id: 2, file: "Job Submission test 2"

我在 rails 控制台中测试了 运行 这段代码。

To search for Job Submission with the job id of 1.

irb(main):013:0> JobSubmission.ransack({"by_job" => 1}).result
Traceback (most recent call last):
        2: from (irb):13
        1: from app/models/job_submission.rb:6:in `block in <class:JobSubmission>'
ArgumentError (wrong number of arguments (given 0, expected 1))

To search for Job Submission with the job id of 2.

irb(main):014:0> JobSubmission.ransack({"by_job" => 2}).result
  SQL (1.0ms)  SELECT  "job_submissions"."id" AS t0_r0, "job_submissions"."job_id" AS t0_r1, "job_submissions"."file" AS t0_r2, "job_submissions"."created_at" AS t0_r3, "job_submissions"."updated_at" AS t0_r4, "jobs"."id" AS t1_r0, "jobs"."job_title" AS t1_r1, "jobs"."published" AS t1_r2, "jobs"."summary_html" AS t1_r3, "jobs"."body_en_html" AS t1_r4, "jobs"."body_ph_html" AS t1_r5, "jobs"."created_at" AS t1_r6, "jobs"."updated_at" AS t1_r7 FROM "job_submissions" LEFT OUTER JOIN "jobs" ON "jobs"."id" = "job_submissions"."job_id" WHERE "jobs"."id" =  LIMIT   [["id", 2], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<JobSubmission id: 2, job_id: 2, file: "Job submission 2", created_at: "2020-10-01 03:14:08", updated_at: "2020-10-01 03:14:08">, #<JobSubmission id: 4, job_id: 2, file: "Job Submission test 2", created_at: "2020-10-01 03:56:10", updated_at: "2020-10-01 03:56:10">]>

我也尝试将我的 gem 搜索更新到 master 分支,但仍然没有解决问题。 提前感谢您的帮助。

我的表格已更新为:

搜索表单

.search-panel
  %h4 Search File
  = search_form_for(@q, url:admin_job_submissions_path, method: :get, as: :q)  do |f|
    = f.label :job_id_eq, "Job:"
    = f.collection_select(:job_id_eq, Job.all, :id, :job_title, :include_blank => "None")

    = f.submit 'Search' , class: "button action-button__new-button"

搜索参数

def search_params
 params.permit(:utf8, :commit, q: [:job_id_eq])
end

并删除 JobSubmission.rb 模型中的作用域

class JobSubmission < ApplicationRecord
  belongs_to :job
end

谢谢大家...

这是由于将 0 转换为 false 以及将 1 转换为 true 造成的。

对于简单的关联 ID,@Mike Alibo 的回答是最好的方法。

但如果任何人必须使用范围来按 id 进行搜索,您可以定义

def self.ransackable_scopes_skip_sanitize_args
  %i[your_scope_name]
end

在你的模型上告诉 ransack 不要投射这个范围的参数。

Docs