A Rails plugin to store a collection of boolean values as a single bit field
As you probably know, boolean values in software systems represent the states “true” or “false”. For some use cases in a software application, you may need a lot of them. by Sebastian Röbke
To give you a very simplified example, imagine a form where the user can check which languages he or she speaks:
Let’s assume that every language option should be stored as a boolean value in a “users” database table, being either “true” or “false”, depending on which of the boxes are checked.
Technically, modern relational database systems such as MySQL can handle many columns per table just fine. However, adding new columns to a table while the database is on load is not that easy if the table already has millions of rows. This is a change that could take hours to complete, depending on the database system and data size. Additionally, there are limits of how many columns can be stored in a single table.
For quite some time, we have been using a nice technique to store boolean values of some large tables more efficiently as a single bit field. Using this approach, we can add new boolean attributes whenever we need to, without needing to alter the database tables, which would cause a downtime. For the part of our system that is based on the Ruby on Rails framework, we have solved this via our own plugin, that we are happy to share:
FlagShihTzu – A rails plugin to store a collection of boolean attributes in a single ActiveRecord column as a bit field.
We have uploaded the plugin with documentation on how it works. It was already covered in episode #5 of the Ruby5 podcast!
As some of our other XING open source projects, the code is hosted on GitHub, a great website to share and collaborate on open source software. So if you have ideas to improve the plugin, please contribute!

Sebastian Röbke works as a Manager Engineering at XING. He loves writing clean, well-tested ruby code.
Uhm, the languages users is a many-to-many relationship, which creates “a problem” which is solved by creating a joined table languages_users, not by adding more attributes to the users table. And certainly not by using bit fields, which violate the first normal form creating lots of problems later on.