Commit a36e20ca by Mai Hoang Thai Ha

add includes to action index of job controller

parent 0e3f626d
......@@ -3,7 +3,7 @@ class JobsController < ApplicationController
if job_params.present?
search
else
@jobs = Job.page(params[:page]).per(20)
@jobs = Job.includes(:company).page(params[:page]).per(20)
end
end
......@@ -17,7 +17,7 @@ class JobsController < ApplicationController
if job_params.key?(:model) && job_params.key?(:slug) # search by model
model = params[:model].classify.constantize
@target = model.find_by(slug: job_params[:slug])
@jobs = @target.jobs.page(params[:page]).per(20)
@jobs = @target.jobs.includes(:company).page(params[:page]).per(20)
elsif job_params.key?(:search) # search by keywords
wildcard_search = "%#{job_params[:search]}%"
@jobs = Job.where('name ILIKE ? OR postal_code LIKE ?', wildcard_search, wildcard_search)
......
......@@ -3,6 +3,6 @@ class TopController < ApplicationController
@total_job = Job.count
@latest_jobs = Job.latest
@top_cities = City.top_jobs.take(City::TOP_JOB_COUNT)
@top_industries = Industry.job_quantity_by_industry.take(Industry::TOP_JOB_COUNT)
@top_industries = Industry.top_jobs.take(Industry::TOP_JOB_COUNT)
end
end
......@@ -13,8 +13,7 @@ class City < ApplicationRecord
def self.job_quantity_by_region
regions.each_with_object({}) do |(name, _), hash|
hash[name.to_sym] = send(name).joins(:jobs).group(:name, :slug)
.order('COUNT(jobs.id) DESC').count
hash[name.to_sym] = send(name).joins(:jobs).group(:name, :slug).count
end
end
end
......@@ -5,7 +5,11 @@ class Industry < ApplicationRecord
validates :slug, presence: true, uniqueness: { case_sensitive: true }
def self.job_quantity_by_industry
def self.top_jobs
joins(:jobs).group(:name, :slug).order('COUNT(jobs.id) DESC').count
end
def self.job_quantity_by_industry
joins(:jobs).group(:name, :slug).count
end
end
......@@ -20,10 +20,10 @@
= link_to job.company.name, '#', class: 'job-company text-decoration-none text-secondary'
p.job-salary.text-success
| Salary: #{job.salary}
ul.list-unstyled
- job.cities.each do |city|
li
= city.name
/ ul.list-unstyled
/ - job.cities.each do |city|
/ li
/ = city.name
.job-desc
= truncate(simple_format(job.description), escape: false, length: 250)
hr.my-4
......
- provide(:title, 'Home page')
/ search box
= render 'shared/search'
.container
.row.text-center
.col
h2.my-4
| Total: #{pluralize(@total_job, "job")}
= render 'shared/search'
/ latest job
.latest-job.mb-5
.container.mb-5
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment