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
@job_count = Job.count
@cities = City.all_cities
@lasted_jobs = Job.order(created_at: :desc).limit(Job::NUMBER_LASTED_JOB)
@top_cities = City.top_hot.take(9)
@top_industries = Industry.top_hot.take(9)
@top_cities = City.top_cities(9)
@top_industries = Industry.top_industries(9)
end
end
......@@ -5,13 +5,9 @@ class City < ApplicationRecord
has_many :city_jobs
has_many :jobs, through: :city_jobs
scope :all_cities, -> { select :id, :name }
def self.top_hot
hash = {}
data_cities = City.all
data_cities.each do |val|
hash[val.name] = val.jobs.count
end
hash.sort_by { |k, v| v }.reverse
scope :top_cities, ->(number) do joins(:jobs)
.group(:city_id)
.order(Arel.sql('count(jobs.id) DESC'))
.take(number)
end
end
......@@ -4,14 +4,9 @@
class Industry < ApplicationRecord
has_many :industry_jobs
has_many :jobs, through: :industry_jobs
def self.top_hot
hash = {}
data_industries = Industry.all
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
scope :top_industries, ->(number) do joins(:jobs)
.group(:industry_id)
.order(Arel.sql('count(jobs.id) DESC'))
.take(number)
end
end
......@@ -4,14 +4,14 @@
<span>Top thành phố</span>
</div>
<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="box_info_city">
<div class="ct_name">
<%= link_to k, '#' ,class: 'link_ct'%>
<%= link_to city.name, '#' ,class: 'link_ct'%>
</div>
<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>
......
......@@ -4,14 +4,14 @@
<span>Top các nghành phổ biến</span>
</div>
<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="box_info_city">
<div class="ct_name">
<%= link_to name, '#',class: 'link_ct'%>
<%= link_to industry.name, '#',class: 'link_ct'%>
</div>
<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>
......
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