Controller
def index
@q = JobSubmission.ransack(search_params[:q])
@job_submissions = @q.result(distinct: true)
@job_submissions = @job_submissions.page(params[:page]).per(10)
end
I created a association between Job and Job Submission
Job.rb model
class Job < ApplicationRecord
has_many :job_submissions
end
JobSubmission.rb model
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 form
.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"
I have 2 Jobs and 4 Job Submissions.
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"
I tested to run this code in my rails console.
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" = $1 LIMIT $2 [["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">]>
I tried also to update my gem ransack to master branch but still didnt fix the issue. Thank you for any help in advance.