ActiveRecord Question: Disabling Boolean Emulation

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.

On Wed, Sep 30, 2009 at 6:37 PM, Kendall G. [email protected]
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/

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.