如何在Rails中实现多对多关系的搜索

时间:2018-05-25 05:09:05

标签: ruby-on-rails ruby

many to manyjobcity个关系class Job < ApplicationRecord has_many :job_cities, dependent: :destroy has_many :cities, through: :job_cities end ,这是我的模特

job.rb

class City < ApplicationRecord
    has_many :job_cities, dependent: :destroy
    has_many :jobs, through: :job_cities
end

city.rb

class JobCity < ApplicationRecord
    belongs_to :city
    belongs_to :job
end

和job_city.rb

jobs

我想你已经理解了发生了什么。现在我需要根据cities&amp; 2.3.4 :002 > city = City.find_by_name('Madrid') City Load (0.3ms) SELECT "cities".* FROM "cities" WHERE "cities"."name" = ? LIMIT ? [["name", "Madrid"], ["LIMIT", 1]] => #<City id: 3, name: "Madrid", created_at: "2017-11-15 13:33:41", updated_at: "2017-11-15 13:33:41", jobs_count: 1, companies_count: 0> 2.3.4 :003 > jobs = Job.includes(:cities).where('job_cities.city_id' => city.id) SQL (0.5ms) SELECT DISTINCT "jobs"."id" FROM "jobs" LEFT OUTER JOIN "job_cities" ON "job_cities"."job_id" = "jobs"."id" LEFT OUTER JOIN "cities" ON "cities"."id" = "job_cities"."city_id" WHERE "job_cities"."city_id" = ? LIMIT ? [["city_id", 3], ["LIMIT", 11]] SQL (0.6ms) SELECT "jobs"."id" AS t0_r0, "jobs"."title" AS t0_r1, "jobs"."details" AS t0_r2, "jobs"."location" AS t0_r3, "jobs"."no_of_vacancies" AS t0_r4, "jobs"."age_range" AS t0_r5, "jobs"."salary_range" AS t0_r6, "jobs"."requirements" AS t0_r7, "jobs"."qualifications" AS t0_r8, "jobs"."additional_qualifications" AS t0_r9, "jobs"."label" AS t0_r10, "jobs"."deadline" AS t0_r11, "jobs"."years_of_experience" AS t0_r12, "jobs"."tags" AS t0_r13, "jobs"."email_for_receive_cv" AS t0_r14, "jobs"."view_count" AS t0_r15, "jobs"."urgency" AS t0_r16, "jobs"."status" AS t0_r17, "jobs"."company_id" AS t0_r18, "jobs"."employer_id" AS t0_r19, "jobs"."created_at" AS t0_r20, "jobs"."updated_at" AS t0_r21, "jobs"."is_hb_apply" AS t0_r22, "jobs"."job_id" AS t0_r23, "jobs"."closed" AS t0_r24, "cities"."id" AS t1_r0, "cities"."name" AS t1_r1, "cities"."created_at" AS t1_r2, "cities"."updated_at" AS t1_r3, "cities"."jobs_count" AS t1_r4, "cities"."companies_count" AS t1_r5 FROM "jobs" LEFT OUTER JOIN "job_cities" ON "job_cities"."job_id" = "jobs"."id" LEFT OUTER JOIN "cities" ON "cities"."id" = "job_cities"."city_id" WHERE "job_cities"."city_id" = ? AND "jobs"."id" = 6 [["city_id", 3]] => #<ActiveRecord::Relation [#<Job id: 6, title: "Android Developer", ...........>]> 然后我尝试了这样的

哪个工作:

2.3.4 :004 > city_not_matched = City.find_by_name('madrid')
  City Load (0.3ms)  SELECT  "cities".* FROM "cities" WHERE "cities"."name" = ? LIMIT ?  [["name", "madrid"], ["LIMIT", 1]]
 => nil 
2.3.4 :005 > jobs = Job.includes(:cities).where('job_cities.city_id' => city_not_matched.id)
NoMethodError: undefined method `id' for nil:NilClass
        from (irb):5

哪个无法正常工作

@city = City.find_by_name(params[:city])
@jobs = Job.includes(:cities).where('job_cities.city_id' => @city.id)

尝试使用控制台,我在搜索控制器中使用的代码与此类似

many to many

摘要是城市名称完全匹配,然后搜索工作,否则显示

  

NoMethodError:未定义的方法`id&#39;为零:NilClass

我知道无法找到城市名称,这就是为什么这个错误,这不是一种正确的搜索方式,而是如何。

我的问题是: 如何使用like关联进行搜索,我希望实现h2关键字搜索而不会出现任何错误。

注意: 请不要告知任何宝石。

由于

1 个答案:

答案 0 :(得分:3)

我会做这样的事情:

Job.joins(:cities)
   .where("cities.name ILIKE ?", "%#{params[:city]}%")