Skip to content

Instantly share code, notes, and snippets.

@karledurante
Created July 14, 2011 12:26
Show Gist options
  • Save karledurante/1082354 to your computer and use it in GitHub Desktop.
Save karledurante/1082354 to your computer and use it in GitHub Desktop.
Example of a tabless model that lets you harness the power of activerecord, without the baggage of a DB table
## A "Tabless" Model that you can extend to get the features and benefits
## of an ActiveRecord model, without having to persist anything to the database
class TablessModel < ActiveRecord::Base
self.abstract_class = true
def self.columns() @columns ||= []; end
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
end
## Now just extend your model and set it's attributes (columns)
class Person < TablessModel
# Tabless model field definitions...
column :name, :string
column :email, :string
#....validates, callbacks, public methods, etc....
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment