move Constant Variable to model, append relationship

parent a8e034a4
class CitiesController < ApplicationController class CitiesController < ApplicationController
VIETNAM = 1
FOREIGN = 0
def index def index
@cities_vietnam = City.location(VIETNAM) @cities_vietnam = City.location(City::VIETNAM)
@cities_international = City.location(FOREIGN) @cities_international = City.location(City::FOREIGN)
end end
end end
...@@ -2,6 +2,9 @@ class City < ApplicationRecord ...@@ -2,6 +2,9 @@ class City < ApplicationRecord
has_many :city_jobs has_many :city_jobs
has_many :jobs, through: :city_jobs has_many :jobs, through: :city_jobs
VIETNAM = 1
FOREIGN = 0
scope :top_city, -> { joins(:jobs).group(:city_id).order('count(job_id) DESC').limit(9) } scope :top_city, -> { joins(:jobs).group(:city_id).order('count(job_id) DESC').limit(9) }
scope :location, ->(number) { joins(:jobs).group(:city_id).order('count(job_id) DESC').where(location: number) } scope :location, ->(number) { joins(:jobs).group(:city_id).order('count(job_id) DESC').where(location: number) }
end end
...@@ -15,7 +15,7 @@ class Job < ApplicationRecord ...@@ -15,7 +15,7 @@ class Job < ApplicationRecord
has_many :histories has_many :histories
has_many :users, through: :histories has_many :users, through: :histories
scope :limit_job, -> { limit(5).order(created_at: :desc) } scope :limit_job, -> { includes(:cities, :company).order(created_at: :desc).limit(5) }
scope :all_job, -> { limit(20).order(created_at: :desc) } scope :all_job, -> { limit(20).order(created_at: :desc) }
def company_name def company_name
......
...@@ -45,9 +45,7 @@ class Crawler ...@@ -45,9 +45,7 @@ class Crawler
cities_relationship = City.where(name: location_relationship) cities_relationship = City.where(name: location_relationship)
city_job_relationship = CityJob.where(job_id: job.id ,city_id: cities_relationship.ids) city_job_relationship = CityJob.where(job_id: job.id ,city_id: cities_relationship.ids)
if city_job_relationship.blank? job.cities << cities_relationship if city_job_relationship.blank?
job.cities << cities_relationship
end
end end
def industry_relationship(row, job) def industry_relationship(row, job)
...@@ -55,9 +53,7 @@ class Crawler ...@@ -55,9 +53,7 @@ class Crawler
industries_relationship = Industry.where(name: industry_relationship) industries_relationship = Industry.where(name: industry_relationship)
industry_job_relationship = IndustryJob.where(job_id: job.id, industry_id: industries_relationship.ids) industry_job_relationship = IndustryJob.where(job_id: job.id, industry_id: industries_relationship.ids)
if industry_job_relationship.blank? job.industries << industries_relationship if industry_job_relationship.blank?
job.industries << industries_relationship
end
end end
def create_job(title, link_page, row, company) def create_job(title, link_page, row, company)
......
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