Delayed job with custom attributes

I am using delayed work 3.0.2 with ActiveRecord and Rails 3.2.3. I have a User model that uses mix_secure_password mixin, so the password is stored only in encrypted form. Now I want to use the delayed work to send a welcome letter, which should contain a copy of the unencrypted password.

When creating an entry, the plaintext password is in user password #. But the deferred work seems to serialize / deserialize the record-only identifier and create a new instance of the model by executing User.find (X). Thus, my plain text password is lost and the user receives an empty password in his email.

How can I say a delayed task to serialize / deserialize user "virtual" attributes, too, which are not stored in the database otherwise?

This is my monkey patch for deferred task 2.x, which worked perfectly.

class ActiveRecord::Base
  def self.yaml_new(klass, tag, val)
    klass.find(val['attributes']['id']).tap do |m|
      val.except("attributes").each_pair{ |k, v| m.send("#{k}=", v) }
    end
  rescue ActiveRecord::RecordNotFound
    raise Delayed::DeserializationError
  end
end

It does not work with deferred task 3.x. I am also not very interested in fixing my monkey patch, as I hope there will be a suitable solution.

+3
source share
1 answer

3.x - ActiveRecord, Psychial YAML deserializer ActiveRecord . , ActiveRecord . , , ActiveRecord ShipmentImport, , attr_accessor 'user_id' / . .

ShipmentImport ActiveRecord :

def encode_with(coder)
  super
  coder['user_id'] = @user_id
end

def init_with(coder)
 super
  @user_id = coder['user_id']
  self
end

ActiveRecord:

Psych.load_tags[['!ruby/ActiveRecord', ShipmentImport.name].join(':')] = ShipmentImport
+2

All Articles