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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Toby Clemson wrote:> 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? > => falseIs date a database attribute? If so, of what type? -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Aug 10, 5:03 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> Toby Clemson wrote: > > 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 > > Is date a database attribute? If so, of what type? > > -- > We develop, watch us RoR, in numbers too big to ignore.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Toby Clemson 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Oh ok - so the string is being typecast to a datetime and so the regular expression fails? On Aug 10, 6:01 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> Toby Clemson 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---