2011-06-08 79 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