What is it?

See the what is it post.

Why should you use it?

  • No migrations needed for new boolean attributes
  • Avoids ALTER TABLE when adding a new boolean field to a model, which can be important for very large tables
  • Only the one integer column needs to be indexed
  • The index is actually meaningful, unlike a boolean column index
  • Bitwise operations are fast, indexes are even faster, and you can use either one
  • Adds scopes to easily access collections of records with specific fields
    • Chained scopes to combine sql for different bit fields for maximum performance
  • Provides condition builder methods for each bit field so you can construct your own custom SQL with the correct bit field values for the list.
    • Table alias option to allow it to work with tables aliased in a query (e.g. custom joins)
  • Has a set_flag_sql method useful when doing mass updates without initializing object for each row.
  • Has a update_flag! method useful when you want to set a flag, but avoid validations and callbacks on the record.

Why is it better than the alternative?

The competition is Michael Grosser’s bitfields, and as always, it depends on your needs. Grosser’s library is less than half the size of flag_shih_tzu, and has far fewer features, and a much smaller compatibility matrix. Some of the differences are laid out below.

For some things however, you only have one option. If you are still on legacy Ruby or legacy Rails, or using jRuby, then use flag_shih_tzu. If you need multiple flag columns on a single model, use flag_shih_tzu. If you want to use it with form builders, it is slightly easier with flag_shih_tzu.

comparison flag_shih_tzu bitfields
Age Mar 2009 Mar 2010
Legacy MRI Support 1.8.7, 1.9.2 None
Current MRI Support 1.9.3, 2.0, 2.1, 2.2 1.9.3 2.0
Current jRuby Support jRuby-1.7, jRuby-9.0 None
Legacy Rails Support 2.3, 3.0, 3.1, 3.2 None
Current Rails Support 2.3, 3.0, 3.1, 3.2, 4.0, 4.1, 4.2 3.2, 4.0, 4.1, 4.2
Stars Stars Stars
Forks Network Network
Downloads (All-time) Total Downloads Total Downloads
Downloads (Today) Downloads Today Downloads Today
Code Quality Code Quality Code Quality
Build Build Build
Documentation Documentation Documentation
Dependency Status Depfu Unknown
Custom Flag Column Name Yes Yes
Multiple Flag Columns per Model Yes No
Has analog to update_column Yes No
Built-in use with update_all Yes Yes
Built-in use with form builders Yes No
Built-in Validator Yes No
Change Tracking _was No Yes
Change Tracking _changed? Yes Yes
Change Tracking _change No Yes
Scopes Optional Yes Yes