mercredi 14 juin 2017

How to cache a ActiveRecord model object which contains carrierwave uploader to Redis

I have a user model which has a carrierwave uploader for storing ths users profile picture

class User < ActiveRecord::Base
    mount_uploader :profilepic, LogoUploaderUploader

I want to cache this User object in redis. While caching I call the to_json method on the activerecord object which converts the profilepic field in the user object to a hash containing the url of the image

user={id:1, username: "sam", profilepic: {url:"path_to_image.jpg"}}

This part is fine and I can store it in redis. But the trouble comes when I retrieve the data from redis and try to convert it to a ActiveRecord object like the following

user=User.new(json_data)

The profilepic field comes null. Looks like it is not able to deserialize the profilepic hash profilepic: {url:"path_to_image.jpg"}to a proper value.

Is there anyway I can get back the proper value for profilepic after saving to redis.

Aucun commentaire:

Enregistrer un commentaire