ruby - Rails rake task to change column values in database -


a tabel in pgsql database has column named "duration" stores string such as.. "5m 21s".. meaning 5 minutes , 21 seconds.

i creating rake task reformat these total amount of seconds.

so "5m 21s" become "321".

... right write such unsure how build query itself.

desc 'change call duration format' task calls_format_fix: :environment      # service::callsreport.where('duration')...      # nums before 'm' * 60 + nums before 's'.. remove alpha characters.. (do regex? ([0-9]*)m\s+([0-9]*)s)  end 

figured out! code , query comments of doing achieve changing column values. not sure best solution works!

desc 'change call duration format minutes , seconds seconds'  task calls_reformat: :environment     #store calls in durationvalues    durationvalues = service::callsreport.all     #loop through each 'call'    durationvalues.each |call|       #regex capture minutes , seconds      call.duration.match(/([0-9]*)m\s+([0-9]*)s/)       #store first match ($1) , multiply 60 in mintosec      mintosec = $1.to_i * 60       #add mintosec matched seconds regex ($2)      totalvalue = mintosec + $2.to_i       #update value of call duration      call.update(duration: totalvalue)    end end 

Comments