Re: ActiveRecord uses confusing defaults

Jeroen Houben
Fri, 08 Sep 2006 16:17:40 -0000

Andrew Stone wrote:
> I''m not sure if this is what you are after (haven''t woken
up), but I
> wrote a
> little plugin that would nil empty strings before saving the record.
> I''ve
> been using this for over a year without issue.

Yeah I''m using something similar myself, very useful. But newly created
objects just shouldn''t return 0 or "" when I have no such
defaults in my
database. Either I''m doing something wrong or there''s some
oddity in AR.

Could you, just as a test, create this table a a corresponding empty
model and tell me what value Submission.new.postcode returns?

CREATE TABLE `submissions` (
  `id` int(11) NOT NULL auto_increment,
  `email` varchar(255) NOT NULL,
  `fname` varchar(255) NOT NULL,
  `lname` varchar(255) NOT NULL,
  `street` varchar(255) NOT NULL,
  `housenr` int(11) NOT NULL,
  `housenr_ext` varchar(255) default NULL,
  `postcode` varchar(6) NOT NULL,
  `city` varchar(255) NOT NULL,
  `shirt_type` varchar(255) NOT NULL default ''m'',
  `shirt_size` varchar(255) NOT NULL default ''l'',
  `shirt_text` text NOT NULL,
  `processed_at` datetime default NULL,
  `approved_at` datetime default NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Jeroen

p.s. I won''t be reading this forum again untill tomorrow probably