Skip to content

Instantly share code, notes, and snippets.

@knewter
Forked from rclements/gist:1057319
Created June 30, 2011 21:51
Show Gist options
  • Save knewter/1057358 to your computer and use it in GitHub Desktop.
Save knewter/1057358 to your computer and use it in GitHub Desktop.
# Perhaps this
def run
last_id = CatProduct.find(:all, :limit => 1, :order => 'id DESC').first.id
last_id_run = 0
num = 0
while(last_id_run < last_id) do
this_batch = CatProduct.find(:all, :limit => 100, :offset => num, :order => 'id ASC')
GC.start
this_batch.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
last_id_run = this_batch.last.id
num += this_batch.size
end
puts "--------SUCCESS: #{@success_count}-------FAILED: #{@errors.count}--------"
end
@rclements
Copy link

mysql> desc Cat_Products;
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| Product_Key | varchar(50) | NO | MUL | NULL | |
| Product_Cat | varchar(30) | NO | | NULL | |
| Product_Group | varchar(30) | NO | | NULL | |
| Product_Part | varchar(25) | YES | | NULL | |
| Product_Mfg | char(3) | YES | MUL | NULL | |
| Product_Desc | varchar(40) | YES | | NULL | |
| Product_Xtd_Desc | varchar(80) | YES | | NULL | |
| Product_Comments | varchar(100) | YES | | NULL | |
| Product_Special | varchar(120) | YES | | NULL | |
+------------------+--------------+------+-----+---------+-------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment