遅延ジョブを介して body 属性を変更するコールバック メソッドを持つ Post モデル (以下) があります。「遅れ」を取り除けば。#shorten_urls を実行するだけです。すぐに、それは正常に動作します。ただし、遅延ジョブのコンテキストから、更新された本文は保存されません。
class Post < ActiveRecord::Base
after_create :shorten_urls
def shorten_urls
delay.shorten_urls!
end
def shorten_urls!
# this task might take a long time,
# but for this example i'll just change the body to something else
self.body = 'updated body'
save!
end
end
奇妙なことに、ジョブは問題なく処理されます。
[Worker(host:dereks-imac.home pid:76666)] Post#shorten_urls! completed after 0.0021
[Worker(host:dereks-imac.home pid:76666)] 1 jobs processed at 161.7611 j/s, 0 failed ...
それでも、本体は更新されません。私が間違っていることを知っている人はいますか?
- 編集 -
アレックスの提案に従って、コードを次のように更新しました (ただし、役に立ちません)。
class Post < ActiveRecord::Base
after_create :shorten_urls
def self.shorten_urls!(post_id=nil)
post = Post.find(post_id)
post.body = 'it worked'
post.save!
end
def shorten_urls
Post.delay.shorten_urls!(self.id)
end
end