Skip to content

Instantly share code, notes, and snippets.

@lukemelia
Created April 14, 2010 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukemelia/366053 to your computer and use it in GitHub Desktop.
Save lukemelia/366053 to your computer and use it in GitHub Desktop.
ActiveRecord::Base.class_eval do
# ActiveRecord looks up the columns for a table each time it loads a class using that table
# With STI, this means repeated calls to 'SHOW FIELDS FROM table_name' for each subclass.
def self.columns
@@table_columns ||= {}
unless @@table_columns[table_name]
retrieved_columns = connection.columns(table_name, "#{name} Columns")
retrieved_columns.each { |column| column.primary = column.name == primary_key }
@@table_columns[table_name] = retrieved_columns
end
@@table_columns[table_name]
end
#original code we are overriding at the time that I wrote this:
# def columns
# unless defined?(@columns) && @columns
# @columns = connection.columns(table_name, "#{name} Columns")
# @columns.each { |column| column.primary = column.name == primary_key }
# end
# @columns
# end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment