Created
July 23, 2011 10:04
-
-
Save matpowel/1101257 to your computer and use it in GitHub Desktop.
An ActiveRecord emulating Tableless model which works with Rails 3.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Thanks for this gist, I made this demo with it: https://github.com/stevemartin/tableless_form
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
This all works for me.. note I removed your initialize method as it's superfluous, that's what the default one will do but without the unsafe eval.
And then you can use them as normal, except that ARel won't chain the has many through very well, I don't know whether your stuff_things etc are actually real AR models, if they are it will work normally.