Tanin
Tanin

Reputation: 1933

ActiveRecord in Rails 3.0.3 turns the 8th field of MySQL into a BigDecimal. How to fix it?

I have the member table which has 9 fields: id,email,... so on.

member_type is the 8th field

The 8th field is always converted to decimal, no matter what name it is or what type it is.

Here is some experimenting I have done:

irb(main):010:0> Member.all()[0].attributes

=> {"created_date"=>nil, "email"=>"[email protected]", "id"=>1, "is_admin"=>0, "
member_type"=>#<BigDecimal:4f87ce0,'0.0',4(8)>, "name"=>"tanin", "password"=>"3c
f622832f10a313cb74a59e6032f115", "profile_picture_path"=>"aaaaa", "status"=>"APP
ROVED"}

Please notice :member_type, which is the 8th field.

Now if I query only some fields, the result is correct:

irb(main):007:0> Member.all(:select=>"member_type,email")[0].attributes

=> {"email"=>"[email protected]", "member_type"=>"GENERAL"}

I think there must be a bug in ActiveRecord.


Here is some more experiment. I have added "test_8th_field" to be the 8th field and I got this:

irb(main):016:0> Member.all[0].attributes

=> {"created_date"=>nil, "email"=>"[email protected]", "id"=>1, "is_admin"=>0, "
member_type"=>"GENERAL", "name"=>"tanin", "password"=>"3cf622832f10a313cb74a59e6
032f115", "profile_picture_path"=>"aaaaa", "status"=>"APPROVED", "test_8th_field
"=>#<BigDecimal:30c87f0,'0.0',4(8)>}

The 8th field is a BigDecimal (it is a text field in MySQL, though). But the member_type field is amazingly correct this time.

I don't know what is wrong with the number 8...

Please help me.


Here is my schema dump, including test_8th_field:

CREATE TABLE IF NOT EXISTS `members` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `profile_picture_path` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `status` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `is_admin` int(11) NOT NULL,
  `test_8th_field` text COLLATE utf8_unicode_ci NOT NULL,
  `member_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'GENERAL',
  `created_date` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;

Upvotes: 2

Views: 349

Answers (1)

Tanin
Tanin

Reputation: 1933

I have solved it. It turns out that the MySql binary library does not match the version for the MySql database itself.

Upvotes: 3

Related Questions