Created
April 14, 2010 16:59
-
-
Save lukemelia/366053 to your computer and use it in GitHub Desktop.
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
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