Whenever Plugin Help
I am trying to use the Whenever plugin for rails to perform a model process at certain times. My schedule.rb is a开发者_开发技巧s follows:
every 1.day, :at => '5:30 am' do
runner "User.mail_out"
end
My model is as follows:
class User < ActiveRecord::Base
acts_as_authentic
def mail_out
weekday = Date.today.strftime('%A').downcase
@users = find(:conditions => "#{weekday}sub = t")
@users.each { |u| UserMailer.deliver_mail_out(u)}
end
end
When I try to run the script/runner -e development "User.mail_out" command, I get the following error:
/var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:48: undefined method `mail_out' for #<Class:0xb708bd50> (NoMethodError)
from (eval):1
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `eval'
from /var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:48
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/runner:3
Can anyone point out what is going wrong? Also how can I manually invoke the mail_out process (from command line) to test the functionality of my mailing system.
Thank you!
You're getting that error because you've defined the mail_out
method as an instance method instead of a class method. To fix it, change the method definition line to (add self.
):
def self.mail_out
To test it from the command line, do something like this:
script/runner "User.mail_out"
You might want to puts
or print
something so you get some feedback on what happened.
I was getting the same error as the OP, except I did have the Class method defined correctly (or so I thought).
It turned out that I'd put the method in the Controller (which looked like the right place) instead of the Model (which looked like the wrong place). Anyhow, putting the method in the Model fixed this problem for me.
精彩评论