Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6200425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:21:54+00:00 2026-05-24T04:21:54+00:00

I’m trying to send my users an email via cron, each with a list

  • 0

I’m trying to send my users an email via cron, each with a list of their due tasks. I can send an email to each user with overdue tasks. In my email template, I can display ALL overdue tasks. What I can’t do is list only a specific user’s tasks.

First of all, I have created a scope which finds all the users with due tasks:

Users.rb

scope :tasksdue, lambda {
       joins("join tasks on tasks.user_id = users.id").
       where("tasks.dueddate >= ? AND tasks.status = ?", Date.today, false).
       group("users.id")
     }

Also in my users model, I find and send these users an email:

def self.send_reminders
         User.tasksdue.find_each do |user|
         UserMailer.deliver_task_due user 
       end
end

And, in my view, I have this:

<!DOCTYPE html>
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
  </head>
  <body>
    <h1>Hello! <%= @user.name %></h1>

    <% User.listtasks.find_each do |task| %>
  <li> <%= task.tasks.title %> </li>
<% end %>

  </body>

The problem is the tasks due part. I’m calling a scope from my users model:

 scope :listtasks, lambda {
   joins("join tasks on tasks.user_id = users.id").
   where("tasks.dueddate >= ? AND tasks.user_id = ? AND tasks.status = ?", Date.today, :id, false)       
}

Using this, I get an error:

undefined method `title’ for #

I’ve also tried calling from Tasks.rb

 scope :tasksdue, lambda { 
     where("dueddate >= ? AND user_id = ? AND status = ?", Date.today, Task.user_id, false)
  }

Which complains about user_id.

I can get it to list all global overdue tasks, but not per user.

What do I need to do so it only lists the tasks per user??

— EDIT —

As per advice from @ream88 below, I created two independent scopes and in my Tasks controller for open and overdue:

scope :overdue, lambda { where('dueddate >= ?', Date.today) }
scope :open, where(:status => false)

Now I don’t know how to iterate through these. I have tried this in my actionmailer template but the email is the same for each user…

<% Task.open.overdue.find_each do |task| %>
<%= task.title %>
<% end %>

How can I specify user in this request??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:21:56+00:00Added an answer on May 24, 2026 at 4:21 am

    You’re doing a lot of built-in functionally on your own, why? My opinion:

    Create a has_many association in your User model and the corresponding belongs_to association in your Task model (if not already done):

    # user.rb
    has_many :tasks
    
    # task.rb
    belongs_to :user
    

    And add following scopes to Task:

    scope :overdue, lambda { where('dueddate >= ?', Date.today) }
    scope :open, where(:status => false)
    

    And now you’re able to access overdue tasks for a specific user via: user.tasks.open.overdue. (You can probably create only one scope in your Task model including both search criteria, but my opinion is to split my code into the smallest pieces possible.)


    # mailer.rb
    def task_due(user)
      @user, @tasks = user, user.tasks.open.overdue
      mail(:to => user.mail, :subject => 'Do your work! Dammit!') do |as|
        as.html { render('your/email/partial') }
        as.text { render('your/email/partial') }
      end
    end
    
    # your/email/partial.html.erb
    <% @tasks.each do |task| %>
      <li><%= task %></li>
    <% end %>
    
    # your/email/partial.text.erb
    <% @tasks.each do |task| %>
      * <%= task %>
    <% end %>
    
    # models/user.rb
    def self.send_mails_to_lazy_users
      # Remind lazy user about their tasks!
      # This function should be called in a controller, after a specific button was pressed or in a background task, like delayed_job
      includes(:tasks).where(:tasks => ['duedate >= ? AND status = ?', Date.today, false]).each do |lazy_user|
        Mailer.task_due(lazy_user)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.