Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #637 (closed defect: duplicate)

Opened 4 years ago

Last modified 3 years ago

Explicitly setting a boolean attribute on a mysql object results fails: undefined method `to_i'

Reported by: buter@cwts.nl Assigned to: David
Priority: normal Milestone:
Component: ActiveRecord Version: 0.9.5
Severity: normal Keywords: mysql type_case
Cc:

Description

If I explicitly set an attribute that is supposed to be a boolean to a true or false value, it will result in either a

undefined method `to_i' for false:FalseClass

or

undefined method `to_i' for true:TrueClass

The error can be traced back to:

active_record/connection_adapters/abstract_adapter.rb:194:in `type_cast'

Probably the root of the problem lies in the fact that a boolean is encoded as a 'tinyint(1)' in mysql, so the type of the Column is set to :integer.

Change History

02/15/05 22:37:31 changed by anonymous

  • severity changed from critical to normal.

ActiveRecord uses 'show create table' to infer the types of columns. However, even if a column was declared as a bool when the table was created, mysql's "show create table" displays it as a tinyint, thus causing ActiveRecord to consider that the column type is integer. Thus if you say: @faz.a = false, you get the exception from line 194 in abstract_adaptor.rb, which is:

when :integer then value.to_i

mysql> create table faz (a bool); Query OK, 0 rows affected (0.01 sec) mysql> show create table faz; +-------+------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------+ | faz | CREATE TABLE faz (

a tinyint(1) default NULL

) TYPE=MyISAM | +-------+------------------------------------------------------------------+ 1 row in set (0.00 sec)

02/15/05 22:44:04 changed by buter@cwts.nl

Probable work-around should probably be to explicitly cover the case that a 'tinyint(1)' can be a boolean.

03/02/05 03:00:58 changed by nzkoz

  • status changed from new to closed.
  • resolution set to duplicate.

This is fixed in 0.10, see #226