Rails - complex rake task

I had to write a pretty complex rake task this evening.  I made some seriously big changes to my client app and need to run some processes to update some big changes in the database.

The first question I had was where to add a method that the rake task could call.  While this seemed a pretty easy thing to do, a google search led me no where so I went with trial and error.  In the end I found that the method needed to go AFTER the task do block and not in it.

desc 'this is the description of the task'

task do_something: :environment do
 my_objects.all.each do |object|
   object.do_something_cool
 end
end

def do_something_cool
 something_cool
end

The next thing I want to do was to use an existing helper that was in my Rails project.  This was easily found with Google. You simply need to require the helper file and include it.  I added this between the dec and the task do block:

require "#{Rails.root}/app/helpers/my_helper"
include MyHelper

Hope this helps :)

comments powered by Disqus