Tack
Tack

Reputation: 121

Looping through users to send emails

I'm trying to loop through my users who are managers and send them all an email, but I'm getting the following error:undefined method "email" for nil:NilClass. This is how I'm looping, which I think is the source of the issue:

def send_manager_email(current_user)
  managers = User.where(manager: true)
  managers.each do |manager|
    UserMailer.timesheet_notification(@manager, current_user).deliver_now
  end
end

and this is my mailer:

def timesheet_notification(user, current_user)
  @greeting = "Hi"
  @current_user = current_user
  mail to: user.email, subject: "New Timesheet"
end

Where am I going wrong?

Upvotes: 0

Views: 206

Answers (1)

Akshay Borade
Akshay Borade

Reputation: 2472

Please try this. You don't need @manager, You can only use manager when passing parameters.

def send_manager_email(current_user)
      managers = User.where(manager: true)
      managers.each do |manager|
        UserMailer.timesheet_notification(manager, current_user).deliver_now
      end
    end

and this is my mailer:

def timesheet_notification(user, current_user)
  @greeting = "Hi"
  @current_user = current_user
  mail to: user.email, subject: "New Timesheet"
end

Hope this will work for you. Thanks!

Upvotes: 1

Related Questions