2011-06-08 50 views
1

我刚开始研究使用delayed_job gem。Delayed_job in rails失败

为了测试它,我补充说:“推迟”到欢迎电子邮件功能,并改变了呼叫从

UserMailer.welcome_email(self).deliver 

UserMailer.delay.welcome_email(self) 

这就是所谓的用户模型after_create内。在函数执行后,我看到一个条目显示在delayed_job表中。现在,当我运行“耙作业:工作”命令行任务的开始,但给错误如下

[Worker(host:Sanjay-PC pid:7008)] Starting job worker 
[Worker(host:Sanjay-PC pid:7008)] Class#welcome_email failed with NoMethodError: undefined method `welcome_email' for #<Class:0x4871d60> - 0 failed attempts 
[Worker(host:Sanjay-PC pid:7008)] 1 jobs processed at 0.0939 j/s, 1 failed ... 

想,如果我改变了welcome_email方法声明为类方法,如

def self.welcome_email(user) 

(在前面添加自我)可能会有所帮助。但后来当我运行耙作业:工作我得到以下错误

rake aborted! 
undefined method `welcome_email' for class `UserMailer' 
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method' 
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method_chain' 
C:/Ruby192/lib/ruby/gems/1.9.1/gems/delayed_job-2.1.4/lib/delayed/message_sending.rb:50:in `handle_asynchronously' 
c:/mgn/mgn-r3/app/mailers/user_mailer.rb:10:in `<class:UserMailer>' 
c:/mgn/mgn-r3/app/mailers/user_mailer.rb:1:in `<top (required)>' 
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:454:in `load' 
<Stack truncated> 

这似乎现在知道班级为UserMailer但不知何故没有看到类方法welcome_email。

我on Rails的3.0.5,红宝石1.9.2p180和安装delayed_job的宝石2.1.4 - 在Windows

似乎无法在别处找到任何相关的答案。

感谢您的想法。

-S

添加每@ pjammer要求UserMailer代码

class UserMailer < ActionMailer::Base 
    default :from => "[email protected]" 

    def welcome_email(user) 
    @user = user 
    @url = "http://example.com/login" 
    mail(:to => user.email, 
     :subject => "Welcome to My Awesome Site") 
    end 
end 
+0

添加代码为您的邮件类,请,做'UserMailer.welcome_email(个体经营)。交付工作? – pjammer 2011-06-08 23:53:47

回答

1

我的解决办法是在处理类重新定义函数(你这是UserMailer类)

def self.taguri 
    'tag:ruby.yaml.org,2002:class' 
end 

这是一个黑客,我会尽力找到更好的解决方案,但现在它适用于我。

(Rails的3.0.9,1.9.2的Ruby-P290,delayed_job的2.1.4)

2

只要使用,而不是这个

UserMailer.delay.welcome_email(self).deliver 

UserMailer.welcome_email(self).delay.deliver