Created
March 21, 2011 03:15
-
-
Save greg-barnett/878958 to your computer and use it in GitHub Desktop.
Delayed::Job belongs_to owner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# db/migrate/#{timestamp}_add_owner_to_delayed_jobs | |
class AddOwnerToDelayedJobs < ActiveRecord::Migration | |
def self.up | |
add_column(:delayed_jobs, :owner_type, :string) | |
add_column(:delayed_jobs, :owner_id, :integer) | |
end | |
def self.down | |
remove_column(:delayed_jobs, :owner_type) | |
remove_column(:delayed_jobs, :owner_id) | |
end | |
end | |
# config/initializers/delayed_job_config.rb | |
class Delayed::Job < ActiveRecord::Base | |
scope :live, where('failed_at IS NULL') | |
belongs_to :owner, :polymorphic => true | |
end | |
# app/models/foo.rb | |
has_many :bars | |
def do_something_with_bar | |
Delayed::Job.enqueue(self.bars.create!(:spam => 'eggs'), :owner => self) | |
end | |
# app/models/bar.rb | |
belongs_to :foo | |
def perform | |
do_something_time_intensive_with_spam | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks.