Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/delayed/yaml_extensions.rb,
lib/delayed/backend/active_record.rb

Overview

Now, tell YAML how to intelligently load ActiveRecord objects, using the database rather than just serializing their attributes to the YAML. This ensures the object is up to date when we use it in the job.

Direct Known Subclasses

Delayed::Backend::ActiveRecord::Job

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_for_delayed_job(id) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/delayed/backend/active_record.rb', line 2

def self.load_for_delayed_job(id)
  if id
    find(id)
  else
    super
  end
end

.yaml_new(klass, tag, val) ⇒ Object



50
51
52
53
54
# File 'lib/delayed/yaml_extensions.rb', line 50

def self.yaml_new(klass, tag, val)
  klass.find(val)
rescue ActiveRecord::RecordNotFound
  raise Delayed::Backend::RecordNotFound, "Couldn't find #{klass} with id #{val.inspect}"
end

Instance Method Details

#to_yaml(opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/delayed/yaml_extensions.rb', line 41

def to_yaml(opts = {})
  if id.nil?
    raise("Can't serialize unsaved ActiveRecord object for delayed job: #{self.inspect}")
  end
  YAML.quick_emit(self.object_id, opts) do |out|
    out.scalar(taguri, id.to_s)
  end
end