Custom Boolean Definition


I have a legacy database that cannot be changed. The true/false values
in the database are true = -1, false = 0 (Thank you very much VB…)
Unfortunately, when I interact with these, rails/ruby sees everything
as false, which causes all sorts of havoc.

Is there a way to define (in my model or elsewhere) true as -1 instead
of 1?


Is there a way to access the true/false field as an integer so I can
manually test for true/false by

if customer.some_field == -1 then…

Thanks for your help. The lifeline of my project / job depend on
making this work.

You could do something evil like this:

class MyModel < ActiveRecord::Base
def stupid_value
read_attribute(:stupid_value) * -1

def stupid_value=(val)
  write_attribute(:stupid_value, (val * -1))


It’s not a great solution, but it should work.


On Dec 20, 2007 3:50 PM, timo [email protected] wrote:

My books:
Ruby in Practice

My free Ruby e-book

My blogs:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs