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

Ticket #3501 (closed defect: fixed)

Opened 3 years ago

Last modified 3 years ago

"sprint" used as a possible enum value causes problems associated attribute

Reported by: anonymous Assigned to: david@loudthinking.com
Priority: high Milestone:
Component: ActiveRecord Version: 1.0.0
Severity: critical Keywords: fd
Cc:

Description

Background: I had a legacy database that used MySQL enum types. Normally, Rails works fine just by treating these columns as strings. I realized that Rails did not work properly on one of the enum columns that happened to have a possible value of "sprint"

Problem: When an enum type has a possible value of "sprint" the attribute displasy as "0" or blank when accessed. It does not matter whether the actually assigned value of the attribute is set to "sprint" or not.

Quick example to reproduce:

mysql> create table blobs (id int, b enum('value1', 'value2', 'sprint'));
mysql> insert into blobs values (1, 'value1');
mysql> insert into blobs values (2, 'value2');
mysql> insert into blobs values (3, 'sprint');

$ script/generate scaffold blob

Workaround: Removing "sprint" as an available option fixes the problem, but this is not really an optimal workaround for my particular situation.

Environment: Windows XP, ruby 1.8.2, Rails 1.0, MySQL 4.1.13a

Change History

01/16/06 04:04:35 changed by anonymous

  • severity changed from normal to critical.

01/16/06 04:13:32 changed by anonymous

New Information: "sprint" isn't the magic word. It turns out that if an enum value contains any part of "int" then the same behavior happens. Strange.

01/16/06 16:22:24 changed by anonymous

New workaround: I had to redefine the private simplified_type method in ActiveRecord::ConnectionAdapters::Column for my local app. I added the following check.

  # Overriding the auto determination of column types to fix MySQL enums
  def simplified_type(field_type)
    case field_type
    when /enum/i
      :string
    when /int/i
      :integer
    # ... 
    # ... resto of method remains the same ...
  end

Now, this probably doesn't belong in ActiveRecord core, and I am hoping there is a smarter solution that I'm not thinking of.

02/27/06 03:29:04 changed by nzkoz

  • keywords set to fd.

Marking for 1.1 as this is easily fixed and very annoying.

03/18/06 21:49:26 changed by david

  • owner changed from David to michael@koziarski.com.

03/25/06 21:29:37 changed by anonymous

  • owner changed from michael@koziarski.com to david@loudthinking.com.

03/25/06 21:42:29 changed by david

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

(In [4028]) Fixed that MySQL enums should always be returned as strings (closes #3501) [DHH]