I am implementing a feature that reminds users to make a post via email if
- the user has set daily reminders to true
- he has not posted yet today
- and if the current hour matches when he would like to receive the daily reminder in his time zone.
I am going to write a Rake task for it and then schedule it to run every hour.
Currently, the rake tasks looks like this:
namespace :mail do desc "Send daily reminder to users" task :daily_reminder => :environment do User.all.each do |user| Time.zone = user.time_zone if user.reminder == true && Time.current.strftime("%H").to_i == user.reminder_time && user.reminded == false UserMailer.test_email(user).deliver_now user.update_attribute(:reminded, true) elsif Time.current.strftime("%H") == user.reminder_time + 1 && user.reminded == true user.update_attribute(:reminded, false) puts "Update to false" end end end end
I am resetting the field
reminded back to false the hour after they were reminded.
Since I am rather new to Ruby and Ruby on Rails I would appreciate feedback on the setup of this feature a lot.
Please critique away on anything you think is broken or needs improvement.
Also, right now it is not working and it's giving me back a
undefined method + for nil:NilClass which tells me that the user hasn't an ActiveRecord assigned to it? Because the
reminder_time column exists and is set (to 16:00).
- The if clause looks kind of large and unclean. Is that the proper way to go about this?
- Shall I make the whole logic more clean by putting some of the conditions in methods?