# Rake Tasks... undefined method \`get\_alerts'

**URL:** <https://www.ruby-forum.com/t/rake-tasks-undefined-method-get-alerts/121375>\
**Category:** Ruby\
**Created:** [November 27, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/rake-tasks-undefined-method-get-alerts/121375 "2007-11-27T13:04:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [November 27, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/rake-tasks-undefined-method-get-alerts/121375/1 "2007-11-27T13:04:14Z")

</div>

Hi i’m finishing off my RAKE Task to send out alert emails to all the  
active users, and using RAKE Tasks to do this.

I’ve written a task to do the work and placed this in /lib/utils.rake of  
my app.

running it via…

rake RAILS\_ENV=development utils:send\_alerts --trace

works fine…

namespace :utils do  
desc “Find pending alerts and send out emails”  
task(:send\_alerts =\> :environment) do  
for alert in Reminder.get\_alerts  
if alert.done = 0  
puts “Alert #{alert.title} - #{alert.date}”  
UserMailer.deliver\_reminder(alert)  
alert.done = 1  
alert.save!  
end  
end  
end  
end

all that works fine but the actual task: Reminder.get\_alerts

when running it, it says: undefined method `get\_alerts’ for  
Reminder:Class

i can run it via Reminder.find\_all but only want to return certain  
situations, so i’ve placed the task to do it in my Reminder Model but  
that doesn’t work, tried in the controller or helper but still it  
doesn’t see the method.

Any ideas where i should put the ‘get\_alerts’ method?

Appreciate it,

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [November 27, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/rake-tasks-undefined-method-get-alerts/121375/2 "2007-11-27T17:22:51Z")

</div>

Hi,

You’re more likely to get a good range of answers on the Rails list  
(this is the Ruby one):

[http://groups.google.com/group/rubyonrails-talk](http://groups.google.com/group/rubyonrails-talk)

In the meantime I’ve tried to answer your questions:

> namespace :utils do  
> desc “Find pending alerts and send out emails”  
> task(:send\_alerts =\> :environment) do  
> for alert in Reminder.get\_alerts  
> if alert.done = 0

Do you really mean = or do you mean == ?

If alert.done has just two states you’d be better off using a boolean.

> ```
> puts "Alert #{alert.title} - #{alert.date}"
> UserMailer.deliver_reminder(alert)
> alert.done = 1
> alert.save!
> 
> ```

You could replace these two lines with:

alert.update\_attributes! :done =\> 1

> ```
> end
> end
> 
> ```
> 
> end  
> end
> 
> all that works fine but the actual task: Reminder.get\_alerts

Rather than testing alert.done inside your loop, it would be better  
to select only those records you need in Reminder.get\_alerts.

> when running it, it says: undefined method `get\_alerts’ for  
> Reminder:Class
> 
> i can run it via Reminder.find\_all but only want to return certain  
> situations, so i’ve placed the task to do it in my Reminder Model but  
> that doesn’t work, tried in the controller or helper but still it  
> doesn’t see the method.
> 
> Any ideas where i should put the ‘get\_alerts’ method?

It should be in the model. It’s a class level method so you should  
define it like this (there are a couple of other ways; just ensure  
you don’t define it as an instance method):

class Reminder \< ActiveRecord::Base  
def self.get\_alerts  
…  
end  
end

If your reminder model has many alerts, you could simply define the  
association you need:

class Reminder \< ActiveRecord::Base  
has\_many :alerts  
has\_many :outstanding\_alerts,  
:class\_name =\> ‘Alert’,  
:conditions =\> ‘done = 0’  
end

And then your client code can say Reminder.outstanding\_alerts.

Regards,  
Andy S.

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [November 27, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/rake-tasks-undefined-method-get-alerts/121375/3 "2007-11-27T20:29:05Z")

</div>

Excellent!

Works like a charm, thanks for that Andy,
