Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

ActiveRecord: cache column information (See related posts)

// Cache the column information so that no more query will be
// sent to the server thereafter

class ActiveRecord::Base
	
	@@_cached_columns = {}
	class << self
		
		alias :old_columns :columns
		
		def columns
			return @@_cached_columns[table_name] if @@_cached_columns[table_name]
			@@_cached_columns[table_name] = old_columns
		end
		
	end
	
end

Comments on this post

sprsquish posts on Apr 28, 2007 at 01:49
out of curiosity.. why didn't you use

@@_cached_columns[table_name] ||= old_columns
tomafro posts on Apr 28, 2007 at 06:37
Columns are already cached by ActiveRecord. You should only see repeated columns queries when running in development. In production each columns query is only called once.

You need to create an account or log in to post comments to this site.


Click here to browse all 4834 code snippets

Related Posts