# Custom Boolean Definition

**URL:** <https://www.ruby-forum.com/t/custom-boolean-definition/123809>\
**Category:** Rails\
**Created:** [December 20, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/custom-boolean-definition/123809 "2007-12-20T21:51:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![timo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@timo](https://www.ruby-forum.com/u/timo)\
**Post date:** [December 20, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/custom-boolean-definition/123809/1 "2007-12-20T21:51:09Z")

</div>

Greetings.

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?

or

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.

---

<div class="post-metadata">

**Author:** ![timo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@timo](https://www.ruby-forum.com/u/timo)\
**Post date:** [December 20, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/custom-boolean-definition/123809/2 "2007-12-20T21:58:11Z")

</div>

You could do something evil like this:

class MyModel \< ActiveRecord::Base  
def stupid\_value  
read\_attribute(:stupid\_value) \* -1  
end

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

```

end

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

–Jeremy

On Dec 20, 2007 3:50 PM, timo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My books:  
Ruby in Practice

> **[Ruby in Practice](https://www.manning.com/books/ruby-in-practice)**
>
> Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale environments. A cookbook-style reference, it gives you concrete examples of systems...

My free Ruby e-book

> **[Site not found · DreamHost](http://www.humblelittlerubybook.com/)**
>
> The owner of this domain has not yet uploaded their website.

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)
