I have a users object which I get user data from twitter/facebook, then hit save and finally send the user_id and the avatar_url to Resque.enqueue.
self.save!
Resque.enqueue(AvatarPull, {user_id: self.id, url: user[:avatar] }) if user[:avatar] and self.avatar.blank?
Then in self.perform I just use find to get user by id then set the avatar then save.
@queue = :paperclip
def self.perform(options={})
user = User.find(options[:user_id])
user.avatar = URI.parse(options[:url].sub("_normal", ""))
user.save!
end
The problem is I get this error for not finding user by id
(Job{paperclip} | AvatarPull | [{"user_id"=>"51191a5e0eae9cfccf000006", "url"=>"http://source"}]) failed: #<Mongoid::Errors::InvalidFind:
Problem:
Calling Document.find with nil is invalid.
Summary:
Document.find expects the parameters to be 1 or more ids, and will return a single document if 1 id is provided, otherwise an array of documents if multiple ids are provided.
Resolution:
Most likely this is caused by passing parameters directly through to the find, and the parameter either is not present or the key from which it is accessed is incorrect.>
I think it is because the time taken to go to the queue is less than the time it takes to save the record in the database. Is there away to fix this problem, maybe add delay or is there something else?
Resque jobs are stored as JSON packages. What this means is that the options hash that gets passed to the perform method now has keys that are strings and not symbols. Notice the quotes around the “user_id” in the error. Using the string representation of the keys should work: