Created
July 14, 2011 12:26
-
-
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
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
## 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