Skip to content

Instantly share code, notes, and snippets.

@mabsboza
Created September 10, 2024 05:00
Show Gist options
  • Save mabsboza/a3b5f5a465fb33ef91ce5e9dbb35651c to your computer and use it in GitHub Desktop.
Save mabsboza/a3b5f5a465fb33ef91ce5e9dbb35651c to your computer and use it in GitHub Desktop.
class ContractorObject
attr_accessor :activity, :contractor_id
def initialize(activity, contractor_id)
raise ArgumentError, "Activity must not be empty" if activity.nil? || activity.strip.empty?
@contractor_id = contractor_id
@activity = activity
end
end
# Creating Contractor objects from Excel data
contractors = [
ContractorObject.new('PRUEBA', 4048)
]
puts "Total Contractors: #{contractors.count}"
contractors.each_with_index do |contractor, index|
puts "Processing Contractor ##{index + 1}"
contractor_record = Contractor.find_by(id: contractor.contractor_id)
unless contractor_record
puts "Contractor with ID #{contractor.contractor_id} not found."
next
end
activity_record = ContractorType.find_by(name: contractor.activity)
ActiveRecord::Base.transaction do
if activity_record
contractor_record.update(contractor_type_id: activity_record.id)
puts "Updated Contractor ##{contractor_record.id} with Activity ##{activity_record.id}"
else
puts "Activity '#{contractor.activity}' not found for Contractor ##{contractor_record.id}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment