fix csv relationship

parent 52e242c9
Pipeline #814 canceled with stages
in 0 seconds
...@@ -3,6 +3,6 @@ class City < ApplicationRecord ...@@ -3,6 +3,6 @@ class City < ApplicationRecord
has_many :jobs, through: :city_jobs has_many :jobs, through: :city_jobs
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 :vietnam, -> { joins(:jobs).group(:city_id).order('count(job_id) DESC').where('location = 1') } scope :vietnam, -> { joins(:jobs).group(:city_id).order('count(job_id) DESC').where(location: 1) }
scope :international, -> { joins(:jobs).group(:city_id).order('count(job_id) DESC').where('location = 0') } scope :international, -> { joins(:jobs).group(:city_id).order('count(job_id) DESC').where(location: 0) }
end end
...@@ -78,6 +78,7 @@ class Crawler ...@@ -78,6 +78,7 @@ class Crawler
info = Nokogiri::HTML(URI.open("https://careerbuilder.vn/viec-lam/tat-ca-viec-lam-trang-#{n}-vi.html")) info = Nokogiri::HTML(URI.open("https://careerbuilder.vn/viec-lam/tat-ca-viec-lam-trang-#{n}-vi.html"))
links = info.css('div.caption a.company-name').map { |link| link['href'] } links = info.css('div.caption a.company-name').map { |link| link['href'] }
links.each do |link| links.each do |link|
begin
next if link == 'javascript:void(0);' next if link == 'javascript:void(0);'
page = Nokogiri::HTML(URI.open(URI.escape(link))) page = Nokogiri::HTML(URI.open(URI.escape(link)))
name = page.search('p.name')&.text name = page.search('p.name')&.text
...@@ -85,7 +86,6 @@ class Crawler ...@@ -85,7 +86,6 @@ class Crawler
address = page.css('div.content p').children[1]&.text address = page.css('div.content p').children[1]&.text
introduction = page.css('div.main-about-us').text introduction = page.css('div.main-about-us').text
begin
Company.find_or_create_by!(name: name, Company.find_or_create_by!(name: name,
address: address, address: address,
introduction: introduction) introduction: introduction)
......
...@@ -54,21 +54,31 @@ class CSVImporter ...@@ -54,21 +54,31 @@ class CSVImporter
level = row["level"] level = row["level"]
salary = row["salary"] salary = row["salary"]
job = Job.find_or_create_by!(title: title_job,
description: description_job,
level: level,
salary: salary,
company_id: company_id)
industry_name = row["category"] industry_name = row["category"]
industries_relationship = Industry.where(name: industry_name) industries_relationship = Industry.where(name: industry_name)
next if industries_relationship.blank?
industry_job_relationship = IndustryJob.where(job_id: job.id, industry_id: industries_relationship.ids)
next if industry_job_relationship.present?
job.industries << industries_relationship
location_data = row["work place"] location_data = row["work place"]
location = location_data.gsub('["', '').gsub('"]', '') location = location_data.gsub('["', '').gsub('"]', '')
location_relationship = City.where(name: location) location_relationship = City.where(name: location)
next if location_relationship.blank?
location_job_relationship = CityJob.where(job_id: job.id ,city_id: location_relationship.ids)
next if location_job_relationship.present?
Job.find_or_create_by!(title: title_job,
description: description_job,
level: level,
salary: salary,
company_id: company_id) do |job|
job.industries << industries_relationship
job.cities << location_relationship job.cities << location_relationship
end
rescue StandardError => e rescue StandardError => e
@logger.error e.message @logger.error e.message
end end
......
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