Old Guy New Trick

An old guys journey to learn how to code.

My First Rake Task


Author: John on March 29, 2014

At my day job we primarly use Perl for both our scripting and tool type applications as well as using it with a splatter of PHP for some of our web page applications.  I'm trying to get Ruby, and Rails, in the door and see if I can convert others over to Ruby.  I must confess, it is a steep upward battle.

However, I do have a web application on our development server and am preparing to deploy it to production.  It is a fairly simple web application that has three models and will enable us to manage the data elements of our Out-of-Band equipment and the connected devices.

When I first started the development, I manually created some test records.  Then I added a handful more via the seeds.rb file.  But when it came time to populating the database with real data, I needed a better solution.  I had compiled some data in Excel files with over 400 out-of-band devices and over 5,000 connected devices.  No way I was going to manually enter all of those.

This was earlier in the week, and luckily I was able to ask some of the gurus at our weekly Code & Coffee how I could tackle this.  Micah suggested I use the CSV library in Ruby and see about populating the data that way.  He gave me a great nudge in the right direction.

I was fumbling around looking for answers when I saw some information about creating rake tasks.  So I studied up, and created the following rake file:

require 'csv'

oobs_file = "/path/to/file/oob_devices.CSV" devices_file = "/path/to/file/connected_devices.CSV" namespace :oob_import do   task :oobs => :environment do     Oob.delete_all     CSV.foreach(oobs_file, headers: true) do |row|       p row       Oob.create!(row.to_hash)     end   end<   task :devices => :environment do     Device.delete_all     CSV.foreach(devices_file, headers: true) do |row|       oob = Oob.find_by_name("#{row[0]}")       oob.devices.create!(name: "#{row[1]}", pri_port: "#{row[2]}", active: "#{row[3]}", updated_by: "#{row[4]}")       p "Oob_ID: #{oob.id}, name: #{row[1]}, pri_port: #{row[2]}, active: #{row[3]}, updated_by: #{row[4]}"     end   end   task :all => [:oobs, :devices] do     puts "Refreshing Oob and Device Models"   end end

To import my data, I executed the rake task as follows:

rake oob_import:oobs
rake oob_import:devices

I could have used the taks, *oob_import:all* but I wanted to work in two steps so I could watch for errors and fix things while I was building this file.

I can't properly convey the joy and happiness I felt when I saw this new tool work!  It really made my day, especially as it took me a couple of hours of research, trial and error.  But this will make things really easy for me when I move the application to production.

Note that I have the code that starts with "p" in there so I have a nice visual of how the task is running.  If you don't need that visual confirmation, you could remove those two lines of code.

Learn Something New Everyday

Last Edited by: John on December 30, 2015