# ActiveRecord Question: Disabling Boolean Emulation

**URL:** <https://www.ruby-forum.com/t/activerecord-question-disabling-boolean-emulation/176471>\
**Category:** Rails\
**Created:** [October 1, 2009, 1:38am UTC](https://www.ruby-forum.com/t/activerecord-question-disabling-boolean-emulation/176471 "2009-10-01T01:38:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zettabyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b19c9b/32.png) [@zettabyte](https://www.ruby-forum.com/u/zettabyte)\
**Post date:** [October 1, 2009, 1:38am UTC](https://www.ruby-forum.com/t/activerecord-question-disabling-boolean-emulation/176471/1 "2009-10-01T01:38:25Z")

</div>

Hello everyone, I’ve got a question for the ActiveRecord gurus.

ActiveRecord version: 2.3.4  
ActiveRecord adapter: mysql

I’m accessing a database originally designed as the back-end data-  
store for a Microsoft Access-based GUI application (using “linked”  
tables). MS Access, if using linked tables to use a MySQL database  
wants its boolean fields, like ActiveRecord, to by TINYINT(1) fields.  
However, is makes the field “signed” and uses 0 for false and -1 for  
true.

I’ve taken several approaches to interfacing with boolean fields in  
this database in an interoperable way (keeping boolean true values as  
-1). What seems like it should be so easy/simple has yet yield a clean  
solution (to me at least).

My Example Table (tblImages):

ImageID, UNSIGNED INTEGER, AUTOINCREMENT, PRI-KEY  
IsFixed, TINYINT, DEFAULT 0

ActiveRecord class:

class Image \< ActiveRecord::Base  
set\_table\_name ‘tblImages’  
set\_primary\_key ‘ImageID’  
def fixed  
self.IsFixed\_before\_type\_cast == “-1”  
end  
def fixed=(val)  
self[:IsFixed] = (val ? ‘-1’ : ‘0’)  
end  
end

The above code is just one of the many approaches I’ve taken. I always  
get so close yet so far. Reading the value is easy by getting it prior  
to type-casting and interpreting it how I want. However, coercing  
ActiveRecord (and the MySQL adapter) to issue an “UPDATE tblImage SET  
IsFixed = -1 …” seem all but impossible when I issue a save! on an  
instance.

So, is there any way to tell ActiveRecord (or the MySQL adapter) to  
simply disable its boolean emulation on a given field (or even the  
whole table or database)? Or, does anyone have any alternate ideas  
that might work instead or even better?

Thanks for your help!

–  
Kendall G.

---

<div class="post-metadata">

**Author:** ![zettabyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b19c9b/32.png) [@zettabyte](https://www.ruby-forum.com/u/zettabyte)\
**Post date:** [October 1, 2009, 1:47am UTC](https://www.ruby-forum.com/t/activerecord-question-disabling-boolean-emulation/176471/2 "2009-10-01T01:47:47Z")

</div>

On Wed, Sep 30, 2009 at 6:37 PM, Kendall G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> However, coercing  
> ActiveRecord (and the MySQL adapter) to issue an “UPDATE tblImage SET  
> IsFixed = -1 …” seem all but impossible when I issue a save! on an  
> instance.

Why? How are you trying and why does it fail?

You can easily change the values in a before\_save filter.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![zettabyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b19c9b/32.png) [@zettabyte](https://www.ruby-forum.com/u/zettabyte)\
**Post date:** [October 2, 2009, 5:41am UTC](https://www.ruby-forum.com/t/activerecord-question-disabling-boolean-emulation/176471/3 "2009-10-02T05:41:29Z")

</div>

I believe you can do that in the adapter itself. You should be able to  
find it under your activerecord folder (in my PC: C:\ruby\lib\ruby\gems  
\1.8\gems\activerecord-2.2.2\lib\active\_record\connection\_adapters  
\mysql\_adapter.rb). Look for “class MysqlAdapter \< AbstractAdapter” in  
the code and you’ll see this line:

```
  self.emulate_booleans = true

```

Change the line to:

```
  self.emulate_booleans = false

```

and see what happens.
