A serialisable and validatable tableless model to get rid of a few tables and speed things up

I have just published a tiny gem, called tableless_model. It includes some functionality I have used quite often in my recent projects, which can help reduce database complexity, and improve performance, by replacing tables and associations with fewer tables storing serialised data instead.

Fewer tables often mean databases that are easier to manage, as well as improved performance thanks to the reduced number of queries that would otherwise be needed to fetch associated data. I use serialisation in many ways but, in particular, with one-to-one associations between a “parent” model and a “child” model, with the latter only containing information such as options, settings, or debugging data, that uniquely belongs to a single instance of the parent model.

For example’s sake, let’s say you are working on a CMS application, are implementing some SEO optimisations, and want to be able to set custom SEO options for each of the pages managed by the application, with these custom options -if set- overriding some global SEO options that otherwise apply to all of the pages. So the parent model would be a class named Page, while the child model would be a class named SeoOptions, with a one-to-one association between the two of them:


In such cases, the application would normally require two tables in your database -one for each of these models- and either one expensive join or two queries (depending on whether you use :join, :include, or lazy loading) to fetch both a page’s data as well as its SEO options, from these two tables.

Using the tableless_model gem, your could remove the one-to-one association between the two models, and the seo_options table altogether, by storing the SEO options in a column of the pages table, in YAML serialised format. So the models would become:


That’s it. When you now create an instance of SeoOptions, you can get and set its attributes as you would do with a normal model:


Of course, you can also override the default values for the defined attributes when creating a new instance:


Now, if you have used the has_tableless macro in the parent class Page, each instance of Page will store its own SEO options directly -in YAML serialised format- in the column seo.


And this is how the content of the serialised column would look like in the database if you saved the changes as in the example above:


Validations: tableless_model also supports validation methods and callbacks, like for table-based models, through the validatable gem. Note: it currently supports only Rails 2.x syntax. Example:




To install the gem, as usual:


I am planning on writing more on serialisation and other similar tricks that help reduce complexity and/or improve performance, in the meantime I hope you find this tiny gem useful too.

About the author

Vito Botta

I am a passionate web developer based in Espoo, Finland. Besides computing, I love boxing and good food!

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty − 15 =