Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created May 18, 2011 01:47
Show Gist options
  • Save ahoward/977843 to your computer and use it in GitHub Desktop.
Save ahoward/977843 to your computer and use it in GitHub Desktop.
# for some reason active record doesn't include a simple way to serialize to
# cache. this simple trick will take you far
#
# usage:
#
# user = User.find(id)
#
# key = "user-#{ user.id }"
#
# Rails.cache.write(key, user.to_cache)
#
# attributes = Rails.cache.read(key)
#
# user = User.from_cache(attributes)
#
class ActiveRecord::Base
def self.from_cache(attributes = {})
instantiate(attributes)
end
def to_cache
attributes
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment