Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Rake Tasks... undefined method `get_alerts'

Reply
Thread Tools

Rake Tasks... undefined method `get_alerts'

 
 
John Griffiths
Guest
Posts: n/a
 
      11-27-2007
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,
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Andrew Stewart
Guest
Posts: n/a
 
      11-27-2007
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

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 utstanding_alerts,
:class_name => 'Alert',
:conditions => 'done = 0'
end

And then your client code can say Reminder.outstanding_alerts.

Regards,
Andy Stewart

-------
http://airbladesoftware.com


 
Reply With Quote
 
 
 
 
John Griffiths
Guest
Posts: n/a
 
      11-27-2007
Excellent!

Works like a charm, thanks for that Andy,
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
rake: undefined method `to_sym' for [:first_name, :last_name]:Array Patrick Doyle Ruby 1 09-30-2008 07:02 PM
Rake and rake aborted! Rake aborted! undefined method `gem' for main:Object peppermonkey Ruby 1 02-10-2007 04:43 AM
Rake aborted! undefined method `gem' for main:Object peppermonkey Ruby 4 02-09-2007 10:47 PM
Rake PackageTask, undefined method exitstatus warhero Ruby 4 01-11-2007 09:28 PM
Rake 0.7.0 breaks "rake engine_migrate"? Joe Van Dyk Ruby 2 01-31-2006 12:11 AM



Advertisments