Working with Ruby
Hi, I am Jan. This is my old Ruby blog. I still post about Ruby, but I now do it on idiosyncratic-ruby.com. You should also install Irbtools to improve your IRB.

The has_many_booleans Rails plugin

has_many_booleans is an ActiveRecord plugin which creates virtual boolean attributes for a model. When the object gets saved, the plugin transforms all attributes into a single integer, using a bitset. So you can easily add new attributes without changing the database structure.

How to setup

It is hosted on github, so you can easily install it with

  • Rails 2: script/plugin install git://github.com/janlelis/has_many_booleans
  • Rails 3: rails plugin install git://github.com/janlelis/has_many_booleans
  • …or get it as a gem: gem install has_many_booleans (and add it to your environment.rb/Gemfile)

Now you have to add an integer field to your model’s table with the name booleans.

How to use

You can activate the plugin wih something like the following in your model file:

 1
2
3
4
5
6
7
8
9
class Model < ActiveRecord::Base
  has_many_booleans :name, :password
end

# here are some of the methods you can now use:
# a = Model.new
# a.name_activated  # false
# a.name_activated = true
# a.name_activated  # true

For more information, see the README ;)

(c) 2010 Jan Lelis .