Well, I have the following problem: every time the cycle passes, I save each variable in the database but I can not do it. It saves me either an empty record or a record with the code, the type of order and the date in another registry. I already have a CRUD created
This is more or less what I want
if line.start_with?('UNH')
clave = line[0..4]
guadar variable clave em mi base de datos y seguir en el mismo regitro hasta leer la ultima variable
end
if line.start_with?('DTM137')
clave = line[0..5]
fechag = line[6...14]
guardar fecha dentro del mismo registro
end
this is my code
File.foreach('ojo.txt').with_object([]) do |line, result|
if line.start_with?('UNH')
clave = line[0..4]
result << "#{clave} ORDEN DE COMPRA"
end
if line.start_with?('BGM')
clave = line[0..2]
codigooc = line[6...26]
if line.include?('BGM2')
region = 'ORDEN DE COMPRA '
else
region = 'ORDEN DE COMPRA SUGERIDA'
end
if cajas = '1'
region = 'CANCELACION'
end
if cajas = '9'
region = 'ORIGINAL'
end
if cajas = '6'
region = 'PROPUESTA'
end
result << "#{clave} codigo #{region}, #{@codigooc}"
Encabezado.create!(numero_oc: codigooc, tipo_oc: region)
end
if line.start_with?('DTM137')
#line.delete(' ')
clave = line[0..5]
fechag = line[6...14]
result << "#{clave} ,FECHA DE GENERACION MENSAJE #{fechag}"
end
if line.start_with?('DTM ')
clave = line[0..2]
fechaen = line[5...13]
result << "#{clave} , FECHA DE ENTREGA: #{fechaen}"
#Encabezado.create!(fechaen: fechaen)
end
if line.start_with?('DTM157')#
fechalist = line[6...14]
clave = 'DTM157'
result << "#{clave} , FECHA DE LISTA DE PRECIOS: #{fechalist}"
end
if line.start_with?('NADBY')
clave = line[0..4]
comprador = line[6...-1]
result << "#{clave} , codigo comprador #{comprador}"
end
if line.start_with?('NADSU')
clave = line[0..4]
comprador = line[6...-1]
result << "#{clave} , codigo proveedor #{comprador}"
end
if line.start_with?('CTAOC')
clave = line[0..4]
comprador = line[5...-1]
result << "#{clave} , INFORMACION PROVEEDOR #{comprador}"
end
if line.start_with?('PAT')
clave = line[0..2]
tpago = line[5]
dentrega = line[7..8]
dpagar = line [-3..-1]
result << "#{clave} , codigo #{tpago} , condiciones normales #{tpago} , dias para entrega #{dentrega} , Dias para pagar #{dpagar} "
end
if line.start_with?('TODNC')
clave = line[0..4]
flete1 = 'flete por cuenta del vendedor sin costo'
else
flete2 = 'flete por cuenta del comprador'
result << "#{clave} #{flete2}"
end
if line.start_with?('LOC')
clave = line[0..2]
lugar = line[5]
codigolugar = line[6...-1]
result << "#{clave} direccion de entrega #{codigolugar}"
end
image of the database and how to keep the empty records or save what should be a record in two ... try to put the other fields and do not take them