aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/table.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/arel/table.rb')
-rw-r--r--lib/arel/table.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/arel/table.rb b/lib/arel/table.rb
index 06bbe7b99e..d23245691b 100644
--- a/lib/arel/table.rb
+++ b/lib/arel/table.rb
@@ -88,8 +88,24 @@ module Arel
end
def [] name
+ return nil unless table_exists?
+
name = name.to_sym
columns.find { |column| column.name == name }
end
+
+ private
+ def table_exists?
+ @table_exists ||= tables.key?(@name) || engine.connection.table_exists?(name)
+ end
+
+ def tables
+ self.class.table_cache(@engine)
+ end
+
+ @@table_cache = nil
+ def self.table_cache engine # :nodoc:
+ @@table_cache ||= Hash[engine.connection.tables.map { |x| [x,true] }]
+ end
end
end