One question that’s asked quite frequently is how to create a form in a Rails application without a database table behind it. We’ll show you how to do just that in this episode and to do so we’ll use the blog application that we’ve used in the previous couple of episodes.

What we want to do is add a “Share this article” link on each article’s page. This link will take a user to a form on which they can enter information so that they can share an article via email. We don’t want to keep this information, just use it to send the email so how do we create a form and a model without a corresponding database table?

The approach we’ll take will be to create a normal model with a database table behind it and then modify the application to remove the table. We’ll start by generating a scaffold and for this we’ll use Ryan Bates’ nifty scaffold generator.

We have to create a new model as we’ll be submitting a form and creating a new resource. We’ll call the new model Recommendation as by emailing details about an article to someone we’re recommending it to them. The Recommendation model will have fields to hold who the email is from and to, the id of the article that’s being recommended and a field for a message. The related controller will need new and create actions. We can generate the scaffold with:

When we reload the article’s page now the “Share this article” link will be visible and when we click on it we’ll see the new form.

If we were to fill in this form and submit it we’ll create a new recommendation in the database, but in this case we don’t want to save this form to the database just send an email. In Rails applications the create action is generally used to save a new model to the database but there’s nothing forcing us to do that. Instead we’ll just check to see if the new Recommendation is valid.

We’ll roll back the last migration now to drop the recommendations table and see if our form works without it. We’ll remove the migration file too.

terminal

rm db/migrate/*_recommendations.rb

Without the database table removed we get an error when we reload the form, the application complaining, not surprisingly, that it can’t find the recommendations table. This is because ActiveRecord relies on every model having an associated table.

So how are we going to create a model that doesn’t have a database table behind it? There are several potential solutions including various plugins but we’re going to use the method described in an entry on the Code Tunes blog. This shows a techinque that involves overriding a couple of methods in an ActiveRecord model and then manually defining the columns in the model file rather than in the database table. In our Recommendation model we’ll add in the two overridden methods and then use the column class method to define the columns in a similar way to how they’re defined in a migration file.

When we reload the new recommendation page now we’ll see the form again instead of an error, but the columns are now defined in the model class rather than fetched from a database table.

You might be wondering why the recommendation class still inherits from ActiveRecord::Base when we’re not using a database backend. We could quite easily create a model class that isn’t based on ActiveRecord and have it work as Rails is quite decoupled from ActiveRecord, but there are advantages to keeping our model class inheriting from ActiveRecord. For one thing it means we can use its other features such as validations. We can then validate the format of the email addresses and the length of the message using ActiveRecord validations in the model.

With these validations added we’ll see the same Rails error messages we’d see if we had a normal database-backed model if we submit the form with invalid email addresses.

Another reason to keep our tableless model inheriting from ActiveRecord is that we can still make use of associations. Recommendation has an article_id as one of its fields and so we can still use

ruby

belongs_to :article

in the model so that we can still fetch the related Article object whenever we need it.

We now have an ActiveRecord model that behaves much like any other but which doesn’t rely on a database backend as its columns are defined manually in Ruby. If we do accidentally call a method that hits the database and requires a database table we’ll see the same exception raised that we saw earlier saying that the table doesn’t exist and can work around it.

The one part of the application that remains to be done is the code that sends an email when the recommendation form has been fillled in correctly. We won’t write that now as its out of the scope of this episode but if you want to know how it’s done take a look at episode 61 which covers this topic.

While being able to create tableless models is a useful technique you should ask yourself whether you really don’t want to store records in the database. It’s so easy to store user-submitted data like this in a database table that there has to be a good reason not to do so even if you don’t have any immediate use for it. If nothing else it serves as a backup if our emailing system fails.