Rails SQL Views
Supporting SQL Views in ActiveRecord
Getting started
The Rails SQL Views extension modifies some of the modules in ActiveRecord to add support for SQL Views. It adds the methods create_view and drop_view to the AbstractAdapter so you can use these in migrations. It also modifies the SchemaDumper class to support dumping of create_view logic. Currently the following adapters are supported:
- MySQL
- PostgreSQL (native and pure ruby)
- SQL Server
Here’s how to get rolling:
-
Get It
There are several ways to get the Rails SQL Views extension:
- Install the Gem
- Get to your command line and type
sudo gem install rails_sql_viewson Linux or OS X or typegem install rails_sql_viewson Windows. Rails SQL Views depends on ActiveRecord. If necessary you may have to approve the installation of these dependencies if they are not already installed. - Download
- You can also download the packages in Zip, Gzip, or Gem format from the ActiveWarehouse files section on RubyForge.
- Plugin
- You can install Rails SQL Views as a plugin if you have the Subversion command line client, with the following command:
script/plugin install svn://rubyforge.org/var/svn/activewarehouse/rails_sql_views/trunk - Subversion Checkout
- For the brave you can get the latest code from the Subversion repository. To get the code from the subversion repository you may use the following command line:
svn co svn://rubyforge.org/var/svn/activewarehouse/rails_sql_views/trunk.
-
Require Statements
If you are using the Gem version, then add the following to your
config/environment.rb:require_gem 'rails_sql_views' require 'rails_sql_views'
If you are using the plugin then you don't need to do anything.
-
Use It!
You can now use the library:
class CreatePersonView < ActiveRecord::Migration def self.up create_view :v_people, "select * from people" do |t| t.column :id t.column :name t.column :social_security end end def self.down drop_view :v_people end end
What's There Now?
Right now the library does the following:
- Adds create_view and drop_view methods to Rails schema code
- Makes ActiveRecord::SchemaDumper view-aware
- Adds a supports_views? abstract method to ActiveRecord::ConnectionAdapters::AbstractAdapter which returns false by default
- Changes the MySQL connection adapter to use SHOW TABLE STATUS rather than SHOW TABLES, so that the
tablesmethod returns only tables. Views are returned through theviewsmethod - Implements view support for MySQL, PostgreSQL and SQL Server
- Unit tests included
Copyright 2006-2007, All Rights Reserved
Anthony's Email:anthonyeden@gmail.com
Anthony's Email:anthonyeden@gmail.com