My Profile Photo

Laszlo Balogh


wannabe Software Developer, Engineer, Runner, Photographer and some other things in between...


How to add our own task to Rakefile

This post would not have been possible, have Robbie not shared the initial details to set us to the right direction. Thank you Robbie!

Still just started on our Mod2 project Bike share and as we were tackling one challenge after another, we came across the requirement to import data into our database tables from external CSV files. The requirement called for updating our seed file and as it is we tried to implement it as a new rake task.

For reference, here is our folder structure. We had to work with two files: seeds.rb and our Rakefile.

Project folder structure

Starting with an empty Stations table within our database, we were expting to be able to load all 70 rows from our CSV file and set up the framework to load later on much larger datasets from other CSV files. We used a tool tux to check database table content as manual verification of our steps.

Empty database table

In order to implement the import functionality, we needed two things:

  1. Add our own import_csv method into our seeds.rb file
  2. Create a task in our Rakefile and link it to the method in our seeds.rb

Step 1: Creating our method in seeds.rb

A few key details we had to pay attention on here:
* make sure to require all dependencies, such as CSV and our models
* define our method as we would in any Ruby class

require 'csv'
require_relative '../app/models/station'
require_relative '../app/models/city'

def import_station_csv
  # CODE TO PARSE INFORMATION FROM THE CSV FILE
  # AND
  # WRITE IT TO THE DATABASE Table

  puts "Imported Stations to Table." # Our short message in the terminal
end

Step 2: Link our new method from the Rakefile

We had to define a new task by adding the following piece of code with paying attention to the following key details:
* Create the appropriate link to our seeds.rb file through require_relative
* Add desc so our new task will have its own description in the task list (see screenshot later in this post)
* Define our Rakefile task name through task, we called it import_csv
* Call our CSV file import method, in this particular case import_station_csv * Adding our new task under namespace :db would make our task to be called as rake db:import_csv. Defining our new task outside of this namespace would make it avaialble as rake import_csv

namespace :db do
  require_relative "db/seeds"
  desc "Import CSV to table"
  task :import_csv => :environment do
    import_station_csv
  end
end

In order to confirm that our new task is available with all other rake tasks, called rake --task in our terminal:

List rake tasks in terminal

Only two more steps left:

  1. Run our new rake task: rake db:import_csv
  2. Confirm through tux that our database table Stations have been successfully updated with all 70 entries

Run our own rake task and confirm result with tux

Many thanks again for Robbie for setting us to the right direction and for my project team to work through the implementation!

Keep on coding!!! :-)

comments powered by Disqus