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.
- Github Clone
- For the brave you can get the latest code from the Github repository:
git clone git://github.com/aeden/rails_sql_views.git.
-
Require Statements
Add the following to your
config/environment.rb:require 'rubygems' require 'rails_sql_views'
-
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-2009, All Rights Reserved
Anthony's Email:anthonyeden@gmail.com
Anthony's Email:anthonyeden@gmail.com