DEPRECATION NOTICE: This gem is no longer maintained, and with Rails 3.0 bringing ActionModel::Validations, this gem has surely become useless. If you can’t wait, check out the excellent Validatable gem from Jay Fields in the meanwhile.
First made popular on the (now extinct) RailsWeenie.com fourms, the tableless model has been very popular. Why a tableless model? Read on…
ActiveRecord::Tableless allows you to create a ActiveRecord model that is not associated with a table in your database. This is a great way to exploit ActiveRecord’s validation features for simple tasks like validating the content of a contact form before sending an email.
Another great use for tableless models is mocking. I use ActiveRecord::Tableless extensively in the specs I write for some of my own ActiveRecord mixin’s without worrying about the tables or test databases being setup.
It works great for non-Rails applications too, just in case
The original forum post detailing the code is no longer available, not even in Google’s cache search. So unfortunately I cannot credit the original authors fully. The code does however appear on several other sites and is roughly the same than the code provided here.
$ git clone git://github.com/kennethkalmer/activerecord-tableless-models.git $ gem build activerecord-tableless-models.gemspec $ sudo gem install activerecord-tableless-1.0.0.gem
Rails users do the following inside any Rails project
$ cd vendor/plugins $ gem unpack activerecord-tableless
Restart the application and you’re off.
Derailed applications can unpack the gem in a suitable location or use rubygems
require 'rubygems' require 'tableless'
Define a model like this:
class ContactMessage < ActiveRecord::Base has_no_table column :name, :string column :email, :string validates_presence_of :name, :email end
You can now use the model in a view like this:
<%= form_for :message, @message do |f| %> Your name: <%= f.text_field :name %> Your email: <%= f.text_field :email %> <% end %>
And in the controller:
def message @message = ContactMessage.new if request.post? @message.attributes = params[:message] if @message.valid? # Process the message... end end end
$ git clone git://github.com/kennethkalmer/activerecord-tableless-models.git
REMINDER: This gem is deprecated.