URL の存在をチェックし、存在しない場合は写真オブジェクトを作成しようとする単純なメソッドがあります。
def create_profile_photo(user_id,image_url)
if Photo.find_by_external_url(image_url).blank?
profile_photo = Photo.new
profile_photo.user_id = user_id
profile_photo.picture_from_url(image_url)
profile_photo.save
end
end
# Photo.rb
def picture_from_url(url)
self.external_url = url
self.attachment = URI.parse(clean_url)
end
それは機能します。しかし、delayed_job
それを使用してメソッドを起動しようとすると機能しません。
ruby-1.9.2-p290 :085 > MyClass.new.delay.create_profile_photo(347,'http://www.google.com/images/srpr/logo3w.png')
(0.4ms) COMMIT
=> #<Delayed::Backend::ActiveRecord::Job id: 42, priority: 0, attempts: 0, handler: "--- !ruby/object:Delayed::ProfileableMethod\nobject:...", last_error: nil, run_at: "2012-12-08 21:40:06", locked_at: nil, failed_at: nil, locked_by: nil, queue: nil, created_at: "2012-12-08 21:40:06", updated_at: "2012-12-08 21:40:06">
ruby-1.9.2-p290 :085 > Photo.count
(0.3ms) SELECT COUNT(*) FROM "photos"
=> 0
何か案が?