Skip to content

Instantly share code, notes, and snippets.

@rubypanther
Created February 25, 2011 10:31
Show Gist options
  • Save rubypanther/843621 to your computer and use it in GitHub Desktop.
Save rubypanther/843621 to your computer and use it in GitHub Desktop.
Monkey patch to make postgres views work with rails 3.x
class ApplicationController < ActionController::Base
require 'postgresql_view_monkey'
end
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
@rubypanther
Copy link
Author

Unlike most monkeys, this one can't be used in an initializer, it needs to be loaded later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment