Rake Tasks... undefined method `get_alerts'

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/.

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

And then your client code can say Reminder.outstanding_alerts.

Regards,
Andy Stewart

···

-------

Excellent!

Works like a charm, thanks for that Andy,

···

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