Commit de86016d by Xuan Trung Le

optimize code

parent eeeefc0a
...@@ -9,7 +9,6 @@ class Job < ApplicationRecord ...@@ -9,7 +9,6 @@ class Job < ApplicationRecord
def self.create_new_jobs(arr_jobs) def self.create_new_jobs(arr_jobs)
arr_jobs.each do |item| arr_jobs.each do |item|
job_cities = []
city_names = [] city_names = []
job_industries = [] job_industries = []
industry_names = [] industry_names = []
...@@ -24,27 +23,26 @@ class Job < ApplicationRecord ...@@ -24,27 +23,26 @@ class Job < ApplicationRecord
# City # City
unless item[:city].blank? unless item[:city].blank?
city_names = item[:city].split(',').map(&:strip) city_names = item[:city].split(',').map(&:strip)
job.cities << City.where(name: city_names)
job_cities = City.where(name: city_names)
job_cities.each do |city|
job.cities << city
end
end end
# Company # Company
job.company = Company.find_or_create_by(name: item[:company_name]) company = Company.find_by(name: item[:company_name])
job.company.location = item[:company_location] job.company =
job.company.description = item[:company_description] if company.nil?
Company.create(name: item[:company_name],
location: item[:company_location],
description: item[:company_description])
else
company
end
job.company.cities = job.cities job.company.cities = job.cities
# Industry # Industry
unless item[:industry].blank? unless item[:industry].blank?
industry_names = item[:industry].split(',').map(&:strip) industry_names = item[:industry].split(',').map(&:strip)
job_industries = Industry.where(name: industry_names) job_industries = Industry.where(name: industry_names)
job.industries << job_industries
job_industries.each do |industry|
job.industries << industry
end
industry_names = industry_names - job_industries.pluck(:name) industry_names = industry_names - job_industries.pluck(:name)
industry_names.each do |name| industry_names.each do |name|
......
class CreateJoinTableJobsCites < ActiveRecord::Migration[5.1] class CreateJoinTableJobsCites < ActiveRecord::Migration[5.1]
def change def change
create_join_table :jobs_cities do |t| create_table :cities_jobs do |t|
t.references :job, index: true t.references :job, index: true
t.references :city, index: true t.references :city, index: true
end end
......
class CreateJoinTableCitiesCompanies < ActiveRecord::Migration[5.1] class CreateJoinTableCitiesCompanies < ActiveRecord::Migration[5.1]
def change def change
create_join_table :cities_companies, id: false do |t| create_table :cities_companies, id: false do |t|
t.references :company, index: true t.references :company, index: true
t.references :city, index: true t.references :city, index: true
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