Topic: Updating serialized objects

Hi all,

I have noticed something strange : serialized objects are updated wether they have been modified or not.

Example under rails 3.0.3 using mysql2 gem:

I have a table :

CREATE TABLE IF NOT EXISTS `foos` (
  `var1` text COLLATE utf8_unicode_ci NOT NULL,
  `var2` text COLLATE utf8_unicode_ci NOT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;

I have a model :


class Foo < ActiveRecord::Base
  serialize :var2
end

Under console i type :

> f=Foo.create :var1 => 'bar', :var2 => [1,2,3]

To build the object

then

> f.var2=[4,5,6]
> f.save

development.log says :
UPDATE `foos` SET `var2` = '--- \n- 4\n- 5\n- 7\n' WHERE (`foos`.`id` = 5)
witch is normal

but if you type

> f.var1='foobar'
> f.save

development.log says :
UPDATE `foos` SET `var2` = '--- \n- 4\n- 5\n- 7\n', `var1` = 'foobar' WHERE (`foos`.`id` = 5)
witch is wired because only var1 was modified.

Anybody have an idea if this is a normal issue or a bug ?

Last edited by czuger (2011-01-06 12:18:42)