Created
May 18, 2011 01:47
-
-
Save ahoward/977843 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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