Skip to content

Instantly share code, notes, and snippets.

@joho
Created January 15, 2009 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joho/47257 to your computer and use it in GitHub Desktop.
Save joho/47257 to your computer and use it in GitHub Desktop.
Lazy load your attributes with ActiveRecord
module LazyLoadingAttributes
def lazy_attribute(attribute_name)
send attribute_name
rescue ActiveRecord::MissingAttributeError
if id
reload
send attribute_name
else
raise "Cannot lazy load attributes without and ID (can't find the original record)"
end
end
end
ActiveRecord::Base.send :include, LazyLoadingAttributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment