Skip to content

Instantly share code, notes, and snippets.

@rclements
Created July 6, 2011 13:52
Show Gist options
  • Select an option

  • Save rclements/1067273 to your computer and use it in GitHub Desktop.

Select an option

Save rclements/1067273 to your computer and use it in GitHub Desktop.
class VehiclePartImporter
def initialize options={}
@failed_links = []
@errors = []
@success_count = 0
end
attr_accessor :errors
# Find or create PartNumber by number based on CatProduct.product_part
# Find or create Vehicles with matching attributes of CatVehicles
# Find PartNumbers with same Product/Vehicle Keys
# Create PartNumberVehicleLink with PartNumber.id and Vehicle.id
def run
CatProduct.find_in_batches(:batch_size => 100) do |products|
GC.start
products.each do |cp|
@category = PnCategory.find_or_create_by_name(cp.Product_Cat)
@subcategory = PnCategory.find_or_create_by_name(:name => cp.Product_Group, :parent_id => @category.id)
@part_number = PartNumber.find_or_create_by_number_and_line_code(:number => cp.Product_Part, :line_code => cp.Product_Mfg)
@part_number.update_attributes(:full_description => cp.Product_Xtd_Desc, :name => cp.Product_Desc)
@category_part_number_link = CategoryPartNumberLink.find_or_create_by_part_number_id_and_pn_category_id(:part_number_id => @part_number.id, :pn_category_id => @subcategory.id)
@matched_vehicle = CatVehicle.find_by_Vehicle_Key(cp.Product_Key)
@vehicle = Vehicle.find_or_create_by_year_id_and_make_name_and_model_name_and_engine_name_and_canonical(:year_id => @matched_vehicle.Vehicle_Year, :make_name => @matched_vehicle.Vehicle_Make, :model_name => @matched_vehicle.Vehicle_Model, :engine_name => @matched_vehicle.Vehicle_Engine, :canonical => true)
PartNumberVehicleLink.find_or_create_by_vehicle_id_and_part_number_id(:vehicle_id => @vehicle.id, :part_number_id => @part_number.id)
@success_count += 1
end
end
puts "--------SUCCESS: #{@success_count}-------FAILED: #{@errors.count}--------"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment