Actionmailer Help Needed

13 views
Skip to first unread message

Jenny Blunt

unread,
Jul 28, 2011, 12:05:01 PM7/28/11
to rubyonra...@googlegroups.com
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.

<!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

--
Posted via http://www.ruby-forum.com/.

Kendall Gifford

unread,
Jul 28, 2011, 6:33:14 PM7/28/11
to rubyonra...@googlegroups.com

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

  </body>

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.

Jenny Blunt

unread,
Jul 29, 2011, 4:02:01 AM7/29/11
to rubyonra...@googlegroups.com
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

Frederick Cheung

unread,
Jul 29, 2011, 4:27:55 AM7/29/11
to Ruby on Rails: Talk


On Jul 29, 9:02 am, Jenny Blunt <li...@ruby-forum.com> wrote:
> 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

Jenny Blunt

unread,
Jul 29, 2011, 5:16:23 AM7/29/11
to rubyonra...@googlegroups.com
Hi there Fred

Thanks for your answer.

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

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

Frederick Cheung

unread,
Jul 29, 2011, 6:22:31 AM7/29/11
to Ruby on Rails: Talk


On Jul 29, 10:16 am, Jenny Blunt <li...@ruby-forum.com> wrote:
> Hi there Fred
>
> Thanks for your answer.
>
> I'm just getting in a pickle with Actionmailer I have to say :(
>
> 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



> Thanks, Jx
>
> --
> Posted viahttp://www.ruby-forum.com/.

Jenny Blunt

unread,
Jul 29, 2011, 7:35:50 AM7/29/11
to rubyonra...@googlegroups.com
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

Jenny Blunt

unread,
Jul 29, 2011, 8:05:42 AM7/29/11
to rubyonra...@googlegroups.com
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])

Frederick Cheung

unread,
Jul 29, 2011, 8:59:43 AM7/29/11
to Ruby on Rails: Talk


On Jul 29, 1:05 pm, Jenny Blunt <li...@ruby-forum.com> wrote:
> 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])
>
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
> --
> Posted viahttp://www.ruby-forum.com/.

Surya

unread,
Jul 29, 2011, 9:11:05 AM7/29/11
to rubyonra...@googlegroups.com
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"

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.




--

 Please consider the environment before printing this email.

Regards,
Surya

Jenny Blunt

unread,
Jul 29, 2011, 12:45:32 PM7/29/11
to rubyonra...@googlegroups.com
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

Frederick Cheung

unread,
Jul 29, 2011, 2:46:19 PM7/29/11
to Ruby on Rails: Talk


On Jul 29, 5:45 pm, Jenny Blunt <li...@ruby-forum.com> wrote:
> 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
>
> Thanks
>
> Jenny
>
> --
> Posted viahttp://www.ruby-forum.com/.

Jenny Blunt

unread,
Jul 30, 2011, 4:53:28 AM7/30/11
to rubyonra...@googlegroups.com
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

Jenny Blunt

unread,
Jul 30, 2011, 7:28:00 AM7/30/11
to rubyonra...@googlegroups.com
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

Frederick Cheung

unread,
Jul 30, 2011, 11:03:51 AM7/30/11
to Ruby on Rails: Talk


On Jul 30, 12:28 pm, Jenny Blunt <li...@ruby-forum.com> wrote:
> 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
Reply all
Reply to author
Forward
0 new messages