2011-03-03 17 views
0

我想打電話給在當語句包含兩個長的命令,但由於某些原因,因爲它的語法的,它會執行兩個指令兩次當它被稱爲:有關在When語句中正確重構的建議?

@email = Email.find(params[:id]) 
    delivery = case @email.mail_type 
    # when "magic_email" these two delayed_jobs perform 2x instead of 1x. Why is that? 
    when "magic_email"  then Delayed::Job.enqueue MagicEmail.new(@email.subject, @email.body) 
           Delayed::Job.enqueue ReferredEmail.new(@email.subject, @email.body) 
    when "org_magic_email" then Delayed::Job.enqueue OrgMagicEmail.new(@email.subject, @email.body) 
    when "all_orgs"   then Delayed::Job.enqueue OrgBlast.new(@email.subject, @email.body) 
    when "all_card_holders" then Delayed::Job.enqueue MassEmail.new(@email.subject, @email.body) 
    end 
    return delivery 

我怎樣才能這樣當我點擊when "magic_email"時,它只會渲染那些延期工作一次

+0

我不知道有一個'然後'。我可以問一下你在用'delivery'做什麼?案件陳述完成後它有價值嗎? – Wukerplank 2011-03-03 13:15:31

+0

我忘了在最後添加返回。在這種方式執行時,它只是對延遲的任何工作進行管理。 – Trip 2011-03-03 13:26:10

回答

1

我曾與下面的例子嘗試這樣:

 

    q = [] 
    a = case 1 
    when 1 then q.push 'ashish' 
       q.push 'kumar' 
    when 2 then q.push 'test' 
    when 4 then q.push 'another test' 
    end 
    puts a.inspect #######["ashish", "kumar"] 


這是工作的罰款。這意味着你的情況 - 何時語法正常。這可能是你有其他問題。

+0

謝謝我結束了把這兩個函數放在一個delayed_job中。 – Trip 2011-03-03 14:18:28

1

您正在致電return delivery並且交付變量可能具有重新調用延遲作業的值。這取決於then語句返回的內容,所以儘可能不要返回任何內容。我相信你想要做延遲的工作,而不是使用函數返回任何東西。

也許你應該只有case,並且不要將它存儲在任何變量中。我的意思是交付變量在這裏沒有用處。