Sending emails on user-selected dates

本秂侑毒 提交于 2021-02-10 17:54:11

问题


My app involves people coming to a home page, entering a name of a bet they've made (for fun), their email address, a date they would like to be reminded, and some details about the bet. I am using the whenever gem to have this run once a day.

bet.rb

class Bet < ActiveRecord::Base
  attr_accessible :details, :email, :name, :reminder, :sent

  # Sends user a reminder if current_date is equal to the reminder date of the bet
  def check_bet
    current_date = Time.now.strftime("%Y-%m-%d").to_s
    @bets = Bet.all
    @bets.each do |bet|
    BetMailer.bet_reminder(bet).deliver and bet.sent = true and bet.save! if bet.reminder.to_s == current_date
    end 
  end
end

schedule.rb

every :day, :at => '5:00pm' do
  runner "Bet.check_bet"
end 

bet_mailer.rb

class BetMailer < ActionMailer::Base
  default from: "from@example.com"

  def bet_reminder(bet)
    @bet = bet

    mail to: bet.email, subject: bet.name + " Reminder"
  end
end

I have been successful in having emails sent when the current_date is equal to the date they wanted to be reminded on (reminder). To test this, I've gone into the rails console, and selected a particular Bet object, and ran the check_bet method on it using:

1.9.2p320 :013 > Bet.last.check_bet
  Bet Load (2.8ms)  SELECT "bets".* FROM "bets" ORDER BY "bets"."id" DESC LIMIT 1
  Bet Load (0.9ms)  SELECT "bets".* FROM "bets" 
   (0.2ms)  BEGIN
   (0.3ms)  COMMIT
   (0.2ms)  BEGIN
   (0.3ms)  COMMIT
   (0.2ms)  BEGIN
   (0.2ms)  COMMIT
 => [#<Bet id: 3, name: "Newsroom", email: "email@email.com", reminder: "2013-07-30", details: "Mac and Will are going to get back together.", sent: false, created_at: "2013-07-29 17:23:13", updated_at: "2013-07-29 17:23:13">, #<Bet id: 4, name: "Testing", email: "email@email.com", reminder: "2013-07-29", details: "This is a test", sent: true, created_at: "2013-07-29 18:38:42", updated_at: "2013-07-29 20:17:34">, #<Bet id: 5, name: "Cheaper iPhone", email: "email@email.com", reminder: "2013-07-29", details: "I bet Dad that there will be a cheaper iphone in th...", sent: true, created_at: "2013-07-29 20:39:33", updated_at: "2013-07-29 20:50:14">, #<Bet id: 6, name: "My grades", email: "email@email.com", reminder: "2013-07-29", details: "My grades this year will be > 84% average", sent: true, created_at: "2013-07-29 20:56:18", updated_at: "2013-07-29 21:14:21">] 

After the terminal completes the above, my email box is full of all of the Bet objects that have their reminder = current_date. This proves the SMTP settings are working, and the code in my views is working fine.

However, when I try running the check_bet method on all of the bet objects, I receive an undefined method error:

1.9.2p320 :016 > Bet.all.check_bet
  Bet Load (1.8ms)  SELECT "bets".* FROM "bets" 
NoMethodError: undefined method `check_bet' for #<Array:0x007ffdd2c8c6c0>
    from (irb):16
    from /Users/bvlaar/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /Users/bvlaar/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /Users/bvlaar/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>

'

In addition, when I run the 'bundle exec whenever' command in terminal, nothing seems to get sent.


回答1:


Bet.all returns an Array, for which the check_bet method is not defined. To invoke check_bet on each instance of Bet, you need to do Bet.all.each {|bet| bet.check_bet} or the cleaner Bet.all.each(&:check_bet) (with nod to @Ryan Bigg).

See also http://guides.rubyonrails.org/active_record_querying.html#retrieving-multiple-objects-in-batches, for some performance considerations.




回答2:


  1. whenever is just a gem which help you generate cron tasks, it won't run any scheduled tasks for you, crontab does.

  2. I think it will be better to run your scheduler per hour like this

    every :hour do
      runner "Bet.check_bet"
    end 
    
  3. Then, in your Bet model class, you can do this:

    class Bet < ActiveRecord::Base
      attr_accessible :details, :email, :name, :reminder, :sent
    
      # Sends user a reminder if current_date is equal to the reminder date of the bet
      def self.check_bet
        current_date = Time.now.strftime("%Y-%m-%d").to_s
        self.where(:reminder => current_date).each do |bet|
          BetMailer.bet_reminder(bet).deliver # later you can use some background job gem like sidekiq, rescue or delayed_job to send email, otherwise the performance might be a issue
          bet.update_attribute(:sent, true)
        end
       end 
     end
    end
    

Thanks



来源:https://stackoverflow.com/questions/17934896/sending-emails-on-user-selected-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!