Mahesh Sharma
Mahesh Sharma

Reputation: 221

Creating a method which executes Automatically after a specified time in Ruby on Rails

I have created a method in controller where i have used action mailer for sending email.I want that method to be executed after every 12 am at night and email to be send to the person who is having a birthday on that day.

Upvotes: 0

Views: 223

Answers (2)

Mihir Kumar Thakur
Mihir Kumar Thakur

Reputation: 393

In lib folder create a class

class Notification
  def self.send_greetings
    # give here your own logic to find users with birthday
    users = User.all
    users.each do |user|
      UsersMailer.weekly_mail(u.email).deliver
    end
  end
end

setup whenever gem

every :day, :at => '12:00am' do
  runner "Notification.send_greetings", :environment => 'development',   :output => 'log/cron.log' 
end 

Upvotes: 0

puneet18
puneet18

Reputation: 4427

Create rake task under lib folder.And add whenever gem to execute rake task at every 12 am.

Upvotes: 1

Related Questions