Question

I have a rake task that is meant to call a Mailer and email certain users that meet a given condition. But, when I call the rake task from the console using rake nagging_email:send I get the following 'ArgumentError: no method name given' and the task does not run. The full console error log can be seen here: https://gist.github.com/srt32/6433024

I have a mailer set up as follows:

class WorkoutMailer < ActionMailer::Base
    def nagging_email(user)
      @user = user
      subject = "What have you done today?"
      @url = 'http://frozen-taiga-7141.herokuapp.com/members/sign_in'

      mail to: @user.email,
        subject: subject.to_s
    end
end

and then a rake task as follows that gets all the users that meet a given condition (being lazy) and then calls the Mailer given that user as a param:

namespace :nagging_email do
  desc "send nagging email to lazy users"
  task :send => :environment do
    daily_nag
  end

  def daily_nag
    users = User.all
    users.each do |user|
      unless last_workout(user) == Date.today
        WorkoutMailer.nagging_email(user).deliver
      end
    end
  end

  def last_workout(user)
    user = user
    last_workout = user.workouts.order("date DESC").limit(1)
    last_workout_date = last_workout.date
    return last_workout_date
  end
end

Any help trying to figure out how run this rake task would be appreciated. Thanks.

Was it helpful?

Solution

You should run rake from terminal, not rails console. If you for some reason want to do it from rails console, you should load tasks like that

require 'rake'
MyRailsApp::Application.load_tasks 
Rake::Task['my_task'].invoke
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top