Skip to content

Instantly share code, notes, and snippets.

@ringe
Created February 16, 2012 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ringe/1848449 to your computer and use it in GitHub Desktop.
Save ringe/1848449 to your computer and use it in GitHub Desktop.
def mama
csv = open("lookahere")
qtys=[]
fail=[]
csv.each_line do |l|
# Replace comma with period for float value
ln = l.gsub(/((?<=,"\d)(,)(?=\d",))/,".")
# Split line on comma
dt = ln.split(",")
b = Biditem.find_by_tu_no(dt[0])
fail << ln if b.blank?
# Parse date
e = Date.strptime(dt[1], "%m.%d.%y") + 12.hours
q = dt[2].gsub(/"(?!")/,'')
# Add notes
n = dt[3..(dt.size-2)].join(", ").gsub(/"(?!")/,'').chomp("\n")
qtys << Quantity.new(biditem_id: (b ? b.id : nil), entered_at: e, quantity: q, notes: n)
end
return qtys, fail
end
job = Job.find(9)
# Remove the old 490 qtys
qty_ids = job.quantities.collect {|q| q.id }
qty_ids = qty_ids[0..489] # Since I inserted wrong data at first (490 lines)
Quantity.destroy(qty_ids)
# Make new ones - don't save to check result first
qtys,fail = mama
# Store to DB
qtys.each {|q| q.save }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment