Commit 4b00c948 by Ngô Trung Hưng

use scope top hot

parent 5bc09eea
Pipeline #765 canceled with stages
in 0 seconds
...@@ -7,7 +7,7 @@ class HomeController < ApplicationController ...@@ -7,7 +7,7 @@ class HomeController < ApplicationController
@job_count = Job.count @job_count = Job.count
@cities = City.all_cities @cities = City.all_cities
@lasted_jobs = Job.order(created_at: :desc).limit(Job::NUMBER_LASTED_JOB) @lasted_jobs = Job.order(created_at: :desc).limit(Job::NUMBER_LASTED_JOB)
@top_cities = City.top_hot.take(9) @top_cities = City.top_cities(9)
@top_industries = Industry.top_hot.take(9) @top_industries = Industry.top_industries(9)
end end
end end
...@@ -5,13 +5,9 @@ class City < ApplicationRecord ...@@ -5,13 +5,9 @@ class City < ApplicationRecord
has_many :city_jobs has_many :city_jobs
has_many :jobs, through: :city_jobs has_many :jobs, through: :city_jobs
scope :all_cities, -> { select :id, :name } scope :all_cities, -> { select :id, :name }
scope :top_cities, ->(number) do joins(:jobs)
def self.top_hot .group(:city_id)
hash = {} .order(Arel.sql('count(jobs.id) DESC'))
data_cities = City.all .take(number)
data_cities.each do |val|
hash[val.name] = val.jobs.count
end
hash.sort_by { |k, v| v }.reverse
end end
end end
...@@ -4,14 +4,9 @@ ...@@ -4,14 +4,9 @@
class Industry < ApplicationRecord class Industry < ApplicationRecord
has_many :industry_jobs has_many :industry_jobs
has_many :jobs, through: :industry_jobs has_many :jobs, through: :industry_jobs
scope :top_industries, ->(number) do joins(:jobs)
def self.top_hot .group(:industry_id)
hash = {} .order(Arel.sql('count(jobs.id) DESC'))
data_industries = Industry.all .take(number)
data_industries.each do |val|
hash[val.name] = val.jobs.count
end
hash = hash.select { |k, v| v.positive? }
hash.sort_by { |k, v| v }.reverse
end end
end end
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
<span>Top thành phố</span> <span>Top thành phố</span>
</div> </div>
<div class="row no-gutters"> <div class="row no-gutters">
<% @top_cities.each do |k,v| %> <% @top_cities.each do |city| %>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12"> <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="box_info_city"> <div class="box_info_city">
<div class="ct_name"> <div class="ct_name">
<%= link_to k, '#' ,class: 'link_ct'%> <%= link_to city.name, '#' ,class: 'link_ct'%>
</div> </div>
<div class="ct_jobs_count"> <div class="ct_jobs_count">
<%= link_to "#{v} công việc", '#' ,class: 'link_ct'%> <%= link_to "#{city.jobs.count} công việc", '#' ,class: 'link_ct'%>
</div> </div>
</div> </div>
</div> </div>
......
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
<span>Top các nghành phổ biến</span> <span>Top các nghành phổ biến</span>
</div> </div>
<div class="row no-gutters"> <div class="row no-gutters">
<% @top_industries.each do |name, jobs_count| %> <% @top_industries.each do |industry| %>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12"> <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="box_info_city"> <div class="box_info_city">
<div class="ct_name"> <div class="ct_name">
<%= link_to name, '#',class: 'link_ct'%> <%= link_to industry.name, '#',class: 'link_ct'%>
</div> </div>
<div class="ct_jobs_count"> <div class="ct_jobs_count">
<%= link_to "#{jobs_count} công việc", '#',class: 'link_ct'%> <%= link_to "#{industry.jobs.count} công việc", '#',class: 'link_ct'%>
</div> </div>
</div> </div>
</div> </div>
......
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