Skip to content

Instantly share code, notes, and snippets.

@moreta
Forked from matpowel/tableless.rb
Created October 29, 2012 12:22
Show Gist options
  • Save moreta/3973240 to your computer and use it in GitHub Desktop.
Save moreta/3973240 to your computer and use it in GitHub Desktop.
An ActiveRecord emulating Tableless model which works with Rails 3.1
class Tableless < ActiveRecord::Base
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new( name.to_s, default, sql_type.to_s, null )
end
def self.columns()
@columns ||= [];
end
def self.columns_hash
h = {}
for c in self.columns
h[c.name] = c
end
return h
end
def self.column_defaults
Hash[self.columns.map{ |col|
[col.name, col.default]
}]
end
def self.descends_from_active_record?
return true
end
def persisted?
return false
end
# override the save method to prevent exceptions
def save( validate = true )
validate ? valid? : true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment