Actionmailer Help Needed

Hello everyone,

Nice to meet you all, am new to the forum.

I'm stuck with rake / actionmailer trying to display a set of found records.

We have a simple actionmailer rake task that is supposed to send a daily email digest of tasks that are due for a specific user. So far, it's working but the email only displays the first message.

In my task model

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

def self.send_reminders       Task.tasksdue.find_each do |task|       TaskMailer.deliver_task_due task end task_mailer.rb

class TaskMailer < ActionMailer::Base   def task_due(task)       recipients @task.user.email       from "email@example.com"       subject "Your report entitled"       sent_on Time.now       content_type "text/html"       body :task => task   end end

In my rake tasks file I have

namespace :cron do   desc "Send email reminders to all users"   task :send_reminders => :environment do     Task.send_reminders   end end

And in my view, task_due.html.erb, I've tried this.

<!DOCTYPE html> <html>   <head>     <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />   </head>   <body>     <h1>Ahoy! <%= @task.responsible %></h1>     <% Task.send_reminders.each do |report| %>       <%= Task.send_reminders.title %>     <% end %>   </body>

This results in a loop, stack level too deep. I think I understand why. Can you help me understand how I display all my records from the found set?

All the best

Jenny Bx

Hello everyone, Nice to meet you all, am new to the forum.

I’m stuck with rake / actionmailer trying to display a set of found records.

We have a simple actionmailer rake task that is supposed to send a daily email digest of tasks that are due for a specific user. So far, it’s working but the email only displays the first message.

In my task model

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

def self.send_reminders Task.tasksdue.find_each do |task| TaskMailer.deliver_task_due task end task_mailer.rb

class TaskMailer < ActionMailer::Base def task_due(task) recipients @task.user.email from “em...@example.com” subject “Your report entitled” sent_on Time.now content_type “text/html” body :task => task end end

In my rake tasks file I have

namespace :cron do desc “Send email reminders to all users” task :send_reminders => :environment do Task.send_reminders end end

And in my view, task_due.html.erb, I’ve tried this.

Ahoy! <%= @task.responsible %>

<% Task.send_reminders.each do |report| %> <%= Task.send_reminders.title %> <% end %>

Yeah, so why are you calling “Task.send_reminders” in your template? I don’t think this is what you want.

This results in a loop, stack level too deep. I think I understand why.

Yeah, you’ve got a recursion loop.

Given you’ve got at least one Task.tasksdue When call Task.send_reminders

  1. Task.send_reminders calls TaskMailer.deliver_task (passing the task instance)
  2. TaskMailer.deliver_task results in the rendering of the task_due.html.erb template
  3. The task_due.html.erb template calls TaskMailer.deliver_task (a.k.a., GOTO #1)

Can you help me understand how I display all my records from the found set?

All the best

Jenny Bx

Do you really want 1 email per matching task? Or do you want one email per user who has at least one matching task? Part of your code is written as if you want the former while part of it is written as if you want the latter.

Hello zettabyte

Thanks for your reply. Am really baffled by this problem - not sure why I can't get my head around it!!

I'm trying to send one email per user with a list of that user's task which are due.

The problem is that I've been following tutorials which don't exactly cover what I'm trying to achieve and am having issues modifying my code accordingly.

Thanks for your help, looking forward to your reply

Jenny x

Hello zettabyte

Thanks for your reply. Am really baffled by this problem - not sure why I can't get my head around it!!

I'm trying to send one email per user with a list of that user's task which are due.

The problem is that I've been following tutorials which don't exactly cover what I'm trying to achieve and am having issues modifying my code accordingly.

It boils down to why is your mailer template (that is supposed to render a single email), calling Task.send_reminders, given that is the method that is supposed to send all the emails ? What are you trying to iterate over in the template ?

Fred

Hi there Fred

Thanks for your answer.

I'm just getting in a pickle with Actionmailer I have to say :frowning:

Am just trying to send a single email to each user. I need the content to list their due tasks.

That's why I was trying to display the found set.

Thanks, Jx

Hi there Fred

Thanks for your answer.

I'm just getting in a pickle with Actionmailer I have to say :frowning:

Am just trying to send a single email to each user. I need the content to list their due tasks.

That's why I was trying to display the found set.

Forgetting about actionmailer for a second, you're going about things slightly backwards. send_reminders is iterating over a set of tasks, sending an email per task - there's nothing you can do in your mailer that is going to get that down to one per user.

Your send_reminders method should first be identifying those users with at least one due task, then iterate over those users. Then you mailer template can iterate over that user's tasks

You might find it helpful to ditch the actionmailer bit for half an hour and make a view / controller that would display all this information (eg one action that displays a list of users with due tasks and then one action that displays the list of tasks for one such user). If you can get that going then transplanting it to a mailer situation should be straightforward

Fred

Hi Fred

We created some controller actions to list all tasks, due and overdue as below:

In our tasks controller:

List current user's due tasks:

@my_due = Task.find(:all, :conditions => ["dueddate <= ? AND user_id = ? AND status = ?", Date.today + 7.days, current_user.id, false], :include => :taskcategories, :order => "dueddate asc")

I actually can't figure out how to list all the users with overdue tasks.

(We're using devise for authentication, hence the current_user bit)

Is that what you mean?

Sorry for the newbie questions...

Jenny

I've tried doing this in my tasks controller to list all users with upcoming tasks but it's not working...

    @task = Task.all      @user = User.find(:all, :conditions => ["@task.dueddate <= ? AND @task.status = ?", Date.today + 7.days, false])

I've tried doing this in my tasks controller to list all users with upcoming tasks but it's not working...

@task = Task\.all
 @user = User\.find\(:all, :conditions =&gt; \[&quot;@task\.dueddate &lt;= ? AND

@task.status = ?", Date.today + 7.days, false])

You would need to join the tasks table. Once you've done that, remember that you conditions are an sql fragment so you can stick conditions on tasks.duedate. You'll also need to use group by or disctinct to not get duplicate users.

Another approach might be to just get all the overdue tasks and collect their users (removing duplicates obviously)

Fred

the code you have written here: @user = User.find(:all, :conditions => [“@task.dueddate <= ? AND @task.status = ?”, Date.today + 7.days, false])

is not correct probably you should check this : http://api.rubyonrails.org/classes/ActiveRecord/Base.html for making conditional statements. According to me it should not be “@task.duedate” inside quotes. else put #{@task.duedate} instead of simple “@task.duedate

Hi

Ok, so I have created the find user action in my controller:

@user = User.joins(:tasks).where("tasks.dueddate <= ? AND tasks.status = ?", Date.today + 7.days, false)

Which works although I need to get the distinct values out now.

What's the next stage to get actionmailer working with this or am I miles off?

Thanks

Jenny

Hi

Ok, so I have created the find user action in my controller:

@user = User.joins(:tasks).where("tasks.dueddate <= ? AND tasks.status = ?", Date.today + 7.days, false)

Which works although I need to get the distinct values out now.

What's the next stage to get actionmailer working with this or am I miles off?

So now i'd iterate over these users (rather than iterating over due tasks) and send one email for each user. in your mailer template, you can iterate over user.tasks.tasksdue

Fred

Hi Fred

This is the part that I really don't understand how to deal with. I don't understand how I link what you've asked me to do above listing the users with due tasks with actionmailer.

Thanks

Ok, I'm nearly there.

Have figured out how to get actionmailer to email those users with tasks due. That was ok after I got my head around it.

I can get the email view to list ALL tasks but not just those owned by a user. In my view, I've tried this:

<% Task.tasksdue.find_each do |task| %>   <li> <%= task.title %> </li> <% end %>

Which calls a scope from my Task model.

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

But I don't get any data out of that. If I get rid of the user_id aspect, it lists all tasks.

How can I adjust that so it works?

Thanks

Jx

Ok, I'm nearly there.

Have figured out how to get actionmailer to email those users with tasks due. That was ok after I got my head around it.

I can get the email view to list ALL tasks but not just those owned by a user. In my view, I've tried this:

<% Task.tasksdue.find_each do |task| %> <li> <%= task.title %> </li> <% end %>

Which calls a scope from my Task model.

Like i said before, if user is the user in question and the association is called tasks then user.tasks.tasksdue is what you want.

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

But I don't get any data out of that. If I get rid of the user_id aspect, it lists all tasks.

If you look at your log files you'd see that that is searching for rows where the user_id is the string 'user_id'. If you want to be able to pass parameters into a scope then you need somelike like lambda {|parameter1, parameter2| ...} and you then to Task.tasksdue(foo,bar)

Fred