Created
February 25, 2011 10:31
-
-
Save rubypanther/843621 to your computer and use it in GitHub Desktop.
Monkey patch to make postgres views work with rails 3.x
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
class ApplicationController < ActionController::Base | |
require 'postgresql_view_monkey' | |
end |
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
module ActiveRecord | |
module ConnectionAdapters | |
class PostgreSQLAdapter | |
def table_exists?(name) | |
name = name.to_s | |
table, schema = name.split('.', 2).reverse # avoid futzing with no schema case | |
if name =~ /^"/ # Handle quoted table names | |
table = name.gsub(/^"|"$/,'') | |
schema = nil | |
end | |
not query(<<-SQL).blank? | |
SELECT TRUE FROM pg_tables, pg_views | |
WHERE (pg_tables.tablename = '#{table}' | |
OR pg_views.viewname = '#{table}') | |
#{schema ? "AND schemaname = '#{schema}'" : ''} LIMIT 1 | |
SQL | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unlike most monkeys, this one can't be used in an initializer, it needs to be loaded later.