AnthonyGalli.com
AnthonyGalli.com

Reputation: 2866

Send email to user with whenever gem?

A user has_many challenges. I want to send email reminders for each challenge.

How do I tell UserMailer to send to @user.email on each day chosen with @challenge.send_email using the whenever gem?

db

create_table "challenges", force: true do |t|
  t.string   "action"
  t.text     "send_email",       default: "---\n- sun\n"
end

create_table "users", force: true do |t|
  t.string   "name"
  t.string   "email"
end

user_mailer.rb (action mailer)

class UserMailer < ApplicationMailer
  def challenge_reminder(challenge)
    @challenge = challenge
    @user_id = @challenge.user_id
    @email = User.find(@user_id).email
    mail to: @email, subject: "It's time to update your #{@challenge.action} Challenge!"
  end
end

reminder.rake (whenever gem)

namespace :challenges do
  desc 'Send email to users with challenges who want reminder'
  task check_challenge_reminder: :environment do
    puts "Sending challenge reminders..."
      Challenge.challenge_reminder
    puts "done."
  end
end

challenge.rb

serialize :send_email, Array

#send email (whenever)
def self.challenge_reminder
  self.all.each do |challenge|
    if challenge.send_email[Date.current.wday] == Date::ABBR_DAYNAMES[Date.current.wday].downcase # this line is correct where sun = sun in the example I just tested, but no email was sent at the appointed hour.
      UserMailer.challenge_reminder(self).deliver_now        
    end
  end
end

enter image description here

I think I wrote reminder.rake wrong or maybe this line self.all.each do |challenge| is incorrect?

UPDATE

heroku run rake 'challenges:challenge_reminder'

heroku run rake 'challenges:challenge_reminder'
Running rake challenges:challenge_reminder on livetochallenge... up, run.6997
rake aborted!
NoMethodError: undefined method `rake' for main:Object
/app/lib/tasks/reminder.rake:4:in `block (2 levels) in <top (required)>'
Tasks: TOP => challenges:challenge_reminder
(See full trace by running task with --trace)

heroku run rake challenges:challenge_reminder --trace

Running rake challenges:challenge_reminder --trace on livetochallenge... up, run.7335
** Invoke challenges:challenge_reminder (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute challenges:challenge_reminder
rake aborted!
NoMethodError: undefined method `rake' for main:Object
/app/lib/tasks/reminder.rake:4:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/task.rb:248:in `call'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/task.rb:248:in `block in execute'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/task.rb:243:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/task.rb:243:in `execute'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/app/vendor/ruby-2.0.0/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/task.rb:180:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/task.rb:173:in `invoke'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/application.rb:150:in `invoke_task'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/application.rb:106:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/application.rb:106:in `block in top_level'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/application.rb:115:in `run_with_threads'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/application.rb:100:in `top_level'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/application.rb:78:in `block in run'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/application.rb:176:in `standard_exception_handling'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/lib/rake/application.rb:75:in `run'
/app/vendor/bundle/ruby/2.0.0/gems/rake-11.1.0/bin/rake:33:in `<top (required)>'
/app/vendor/bundle/ruby/2.0.0/bin/rake:23:in `load'
/app/vendor/bundle/ruby/2.0.0/bin/rake:23:in `<main>'
Tasks: TOP => challenges:challenge_reminder

bundle exec rake challenges:challenge_reminder

rake aborted!
NoMethodError: undefined method `rake' for main:Object
/Users/galli01anthony/Desktop/LiveToChallenge/lib/tasks/reminder.rake:4:in `block (2 levels) in <top (required)>'
/Users/galli01anthony/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `eval'
/Users/galli01anthony/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => challenges:challenge_reminder
(See full trace by running task with --trace)

Upvotes: 0

Views: 428

Answers (1)

Thieu Nguyen
Thieu Nguyen

Reputation: 1078

UPDATED 2016/03/16

Fix @NoMethodError: undefined method rake' for main:Object issue

Please change self.all.each do |challenge| to Challenge.all.each do |challenge|, and be careful with Challenge.all, it will take too much memory when you have many records, try find_each instead of.


I think the condition in self.challenge_remindershould be:

challenge.send_email.include? Date::ABBR_DAYNAMES[Date.current.wday].downcase

Updated:

According to reminder.rake file, the rake command you're using seems to be wrong, it will be:

rake challenges:check_challenge_reminder

For Heroku server, we can't use config/schedule.rb. Heroku provides a Scheduler for these kinds of tasks (Heroku Scheduler).

Upvotes: 1

Related Questions