Skip to content

Instantly share code, notes, and snippets.

@matpowel
Created July 23, 2011 10:04
Show Gist options
  • Save matpowel/1101257 to your computer and use it in GitHub Desktop.
Save matpowel/1101257 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
@stevemartin
Copy link

Thanks for this gist, I made this demo with it: https://github.com/stevemartin/tableless_form

@huda-kh
Copy link

huda-kh commented Nov 4, 2024

Is anyone using this or similar in Rails 7?

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