DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

On Booleans And Database Portability

  • submit to reddit
        From Coda Hale:

On booleans and database portability

Oh, booleans. Simplest and most elusive of database types. How to specify you? Y and N? T and F? 0 or NULL and anything else? If only we knew…

When working on a Rails app that uses different databases (say, SQLite for development/testing and MySQL for production), be sure that your conditions clauses aren’t assuming a particular form of boolean representation.

This will return nothing in SQLite:
@monkeys = Monkey.find(:all,
  :conditions => 'rabid = 0')
But it’ll work in MySQL.
A Solution

Autogenerate that sucker!
@monkeys = Monkey.find(:all,
  :conditions => ['rabid = ?', false])