Weird validates_format_of problem

Hi,

I have an odd problem when trying to match a hyphen in a
validates_format_of validation:

class DataDate < ActiveRecord::Base

validates_format_of :date, :with => /-/

end

If I try to create a new instance either in my test method or in
script/console:

test = DataDate.new(:date => ‘2007-08-10’)
=> #<DataDate id: nil, date: nil, created_at: nil, updated_at: nil>

test.valid?
=> false

I get a ‘is invalid’ error on the date.

Does anyone have any idea why this might be.
script/console and my unit test report $KCODE to be UTF8 - I thought
it might be character encoding but apparently not. I’ve set the
character encoding in my terminal, the class and unit test files and
when using script/console to be UTF8/NONE but neither makes any
difference.

I’m on a Mac, using TextMate which saves the files in UTF8, using edge
rails revision 7277, mysql5.

Any help at all would be really appreciated as I have no idea what
else to do!

Thanks in advance.
Toby

Toby C. wrote:

=> #<DataDate id: nil, date: nil, created_at: nil, updated_at: nil>

test.valid?
=> false

Is date a database attribute? If so, of what type?


We develop, watch us RoR, in numbers too big to ignore.

Well actually I simplified my example here but the database type in
the migration is datetime and I was actually using a correctly
formatted date of ‘2007-01-01 00:00:00’ in my tests.

Toby C. wrote:

Well actually I simplified my example here but the database type in
the migration is datetime and I was actually using a correctly
formatted date of ‘2007-01-01 00:00:00’ in my tests.

Then you want:

validates_format_of :date_before_type_cast, :with => /-/

Also ensure date is an accessible (hash settable) attribute.


We develop, watch us RoR, in numbers too big to ignore.

Oh ok - so the string is being typecast to a datetime and so the
regular expression fails?