Commit de86016d by Xuan Trung Le

optimize code

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