From time to time I find myself implementing a story needing the nice validation features that
come with ActiveRecord and being able to display input errors in a simple way in the views.
I also want to implement the story in a RESTful way (even if this example should probably be
regarded as RESTlike).
But what I don't want is a table in the database.
Implementing a User-Changes-Password-story not long ago was such an occasion and I decided to clean out the domain specifics and post the way it was done and also publish the active_record_tableless plugin I use.
There were a number of good reasons not to use the existing User model directly, but that's long story. As this example is stripped of all the calls to legacy system it may seem like a strange way to do this but I hope you can follow along and understand how you can use active_record_tableless plugin in your own projects.
User wanting to change password submits email address.
After email validation, the user enters passwords as listed below and submits to complete the change.
- old password
- new password
- confirmation of the new password.
You can download the complete source code from here, but I basically created a new rails app and generated a scaffold for a PasswordUpdate like this:
script/generate scaffold --skip-migration PasswordUpdate email:string \
password:string new_password:string new_password_confirmation:string
To be able to use a model without backing it with a database table I use the active_record_tableless plugin:
script/plugin install git://github.com/robinsp/active_record_tableless.git
We wont need some of the actions created by the scaffold generator so
destroyactions were deleted. The generated controller was also cleansed from code returning xml.
After implementing the things we need for the user story implementation the controller looks like this:
class PasswordUpdatesController < ApplicationController
@password_update = PasswordUpdate.new
@password_update = PasswordUpdate.find( params[:id] )
@password_update = PasswordUpdate.find(params[:id])
if @password_update.update_attributes( params[:password_update] )
render :action => "edit"
@password_update = PasswordUpdate.new(
params[:password_update].merge(:password_required => false ) )
@password_update.exists!( @password_update.email_as_id )
redirect_to edit_password_update_url( @password_update )
render :action => "new"
Fairly standard stuff going on in the
updateactions (lines 3-19) but what's going on in
Our user story states that a valid email address is entered into a form and submitted. Our
newaction renders this form with an single text_field and a button that submits to the
createaction. As we don't care about the passwords in this stage, their validations are disabled by setting the
:password_requiredattribute to false (line 23). We'll look closer at this when discussing the model.
If there was something wrong with the submitted email addres the
newaction is rendered and the errors are displayed to the user (using the standard
However, if everything is ok and
@password_update.savereturns true (line 25) we want to behave well and redirect from the successful post.
As our model doesn't have a persistent id (because it doesn't have a database table) we fake this using the
exists!method, provided by active_record_tableless plugin (line 26) and as our model object now has and an id, the redirect using a url helper on line 27 will work fine.
PasswordUpdate model is implemented like this:
class PasswordUpdate < ActiveRecord::Base
tableless :columns => [
:if => :password_required?
:if => :password_required?
# Defaults to true
self.password_required.nil? ? true : self.password_required
raise "No email set" unless self.email
class << self
raise "No id argument" unless id
p = PasswordUpdate.new(:email => decode(id))
alias_method :find, :find_by_id
def encode( str )
def decode( str )
Line 2-6 set up the attributes we need. To control whether validation should be done on passwords we need an attribute for this (line 9). We saw this in the
The validations declared on lines 10-18 are fairly straight forward except that they don't check the passwords if our
password_requiredattribute is false. (Rigorous validation of email format and such has been omitted in this example code.)
On lines 25-28 a util method is provided to convert the email address into a valid string to use in a url.
Lines 30-37 implements finders to fake that our model actually exists as a normal model would when returned from find. The bare minimum for an existing model is that it should have an
new_record?should return false. The active_record_tableless plugin takes care of this for us when we call the
exists!method (line 34).
active_record_tableless plugin on github
The complete Rails 2.1 project that the example above describes is here.
About 2 years ago Jonathan Viney wrote ActiveRecord::Base Without Table. It doesn't seem to be maintained and doesn't support the exists! method described above. It also has a different way of integrating into the models.
Also, if you want to use "ActiveRecord::Base Without Table" with Rails 2.x you need to install it from activerecord-basewithouttable-for-rails-2 where Clinton R. Nixon has graciously been hosting it for some time.