Created
January 15, 2009 04:28
-
-
Save joho/47257 to your computer and use it in GitHub Desktop.
Lazy load your attributes with ActiveRecord
This file contains 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
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