| Path: | static_active_record_context.rb |
| Last Update: | Thu Mar 26 18:20:07 -0600 2009 |
Simple module to extends technoweenie‘s rad ActiveRecordContext plugin svn.techno-weenie.net/projects/plugins/active_record_context/ to permanently cache active record data for the life of the class
As with active_record_context, only finds based on ids are cache hits, however id finders are the majority of calls from associations. If cache hits on fields and methods are needed, refer to acts_as_static_record
class TelephoneCarriers < ActiveRecord::Base extend StaticActiveRecordContext has_many :phone_numbers end
The following would exercise a cache hit
phone_number.telephone_carrier
The static cache is available both inside and outside with_context block, where as the cache for typical records the context is only with the block.
PhoneNumber.with_context {
PhoneNumber.find :all
TelephoneCarriers.find :all
}
phone = PhoneNumber.find_by_id(1) # not a cache hit
phone.telephone_carrier # cache hit
telephone_carrier = TelephoneCarrier.find(1) # cache hit