26 June 2014

I had to a case where I need to test a boolean value that can be [1,'1'] (in mysql), and I found the better way to do this by using ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES to detect it, rather than testing the literal value.

Api doc says

ActiveRecord::ConnectionAdapters::Column
An abstract definition of a column in a table.

Constants
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set

It’s much better to abstract the boolean because backends can store it differently (for example sqlite3 uses t and f).