RailsCasts Pro episodes are now free!
Learn more or hide this
Resources
Note: Ending the rake command with "&" will not work in Windows. Instead You'll need to prefix it with "start" to run the command in the background. See the comments below for details.
def call_rake(task, options = {}) options[:rails_env] ||= Rails.env args = options.map { |n, v| "#{n.to_s.upcase}='#{v}'" } system "/usr/bin/rake #{task} #{args.join(' ')} --trace 2>&1 >> #{Rails.root}/log/rake.log &" end
def deliver call_rake :send_mailing, :mailing_id => params[:id].to_i flash[:notice] = "Delivering mailing" redirect_to mailings_url end
desc "Send mailing" task :send_mailing => :environment do mailing = Mailing.find(ENV["MAILING_ID"]) mailing.deliver end
def deliver sleep 10 # placeholder for sending email update_attribute(:delivered_at, Time.now) end