Skip to content

Instantly share code, notes, and snippets.

@Holek
Created March 6, 2014 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Holek/9390963 to your computer and use it in GitHub Desktop.
Save Holek/9390963 to your computer and use it in GitHub Desktop.
ActiveRecord concern for MSSQL views
# Usage:
#
# class SomeView < ActiveRecord::Base
# include ViewObject
# end
#
# SomeView.view_sql #=> prints out MsSQL view SQL statement
module ViewObject
extend ActiveSupport::Concern
included do
self.primary_key = nil
end
def readonly?
true
end
module ClassMethods
def view_sql
@view_sql ||= self.connection.select_one("SELECT VIEW_DEFINITION as view_definition FROM INFORMATION_SCHEMA.Views WHERE TABLE_NAME = N'#{self.table_name}'")['view_definition']
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment