I''m having a problem extending ActiveRecord with custom validations based on the "Write Custom Validations" recipe from Advanced Rails Recipes. As far as I can tell, I''ve followed the instructions exactly but for some reason this isn''t working and it''s driving me crazy. I''ve tried making the methods in the custom validations module class methods (self.validates...) as well as the way they are below (which is the way they are in the instructions). Any suggestions? Thanks! Gavin *** lib/custom_validations.rb module CustomValidations def validates_phone(*attr_names) attr_names.each do |attr_name| validates_format_of attr_name, :with => /^[\(\)0-9\- \+\.]{10,20} *[extension\.]{0,9} * [0-9]{0,5}$/i, :message => "invalid" end end def validates_email(*attr_names) attr_names.each do |attr_name| validates_format_of attr_name, :with => /(^([^@\s]+)@((?:[-_a-z0-9]+\.)+[a-z]{2,})$)|(^$)/ i, :message => "invalid" end end end ActiveRecord::Base.extend(CustomValidations) *** config/environment.rb # Be sure to restart your server when you modify this file # Uncomment below to force Rails into production mode when # you don''t control web/app server and can''t set it the proper way ENV[''RAILS_ENV''] ||= ''production'' #normal environment.rb stuff end require ''custom_validations'' *** models/user.rb class User < ActiveRecord::Base validates_email :email end *** error from console when starting server /vendor/rails/activerecord/lib/active_record/base.rb:1667:in `method_missing'': undefined method `validates_email'' for #<Class: 0x31ac9c8> (NoMethodError) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-09 19:09 UTC
Re: Problem extending ActiveRecord with custom validations
On 9 Dec 2008, at 19:05, gaveeno wrote:> > I''m having a problem extending ActiveRecord with custom validations > based on the "Write Custom Validations" recipe from Advanced Rails > Recipes. As far as I can tell, I''ve followed the instructions exactly > but for some reason this isn''t working and it''s driving me crazy. > I''ve tried making the methods in the custom validations module class > methods (self.validates...) as well as the way they are below (which > is the way they are in the instructions). > > Any suggestions? >Have you tried requiring it from an initializer, not the bottom of environment.rb ? Fred> Thanks! > Gavin > > *** > lib/custom_validations.rb > > module CustomValidations > def validates_phone(*attr_names) > attr_names.each do |attr_name| > validates_format_of attr_name, > :with => /^[\(\)0-9\- \+\.]{10,20} *[extension\.]{0,9} * > [0-9]{0,5}$/i, > :message => "invalid" > end > end > def validates_email(*attr_names) > attr_names.each do |attr_name| > validates_format_of attr_name, > :with => /(^([^@\s]+)@((?:[-_a-z0-9]+\.)+[a-z]{2,})$)|(^$)/ > i, > :message => "invalid" > end > end > end > ActiveRecord::Base.extend(CustomValidations) > > *** > config/environment.rb > > # Be sure to restart your server when you modify this file > > # Uncomment below to force Rails into production mode when > # you don''t control web/app server and can''t set it the proper way > ENV[''RAILS_ENV''] ||= ''production'' > > #normal environment.rb stuff > > end > > require ''custom_validations'' > > *** > models/user.rb > > class User < ActiveRecord::Base > validates_email :email > end > > *** > error from console when starting server > > /vendor/rails/activerecord/lib/active_record/base.rb:1667:in > `method_missing'': undefined method `validates_email'' for #<Class: > 0x31ac9c8> (NoMethodError) > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks Fred, that worked! I created config/initializers/ custom_validations.rb and moved the require line there from the environment.rb file. It''s not clear to me why this works, and it seems like it''s overkill to create a new file for this, but as long as it works it''s cool with me. Thanks again. -Gavin On Dec 9, 11:09 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9 Dec 2008, at 19:05, gaveeno wrote: > > > > > I''m having a problem extending ActiveRecord with custom validations > > based on the "Write Custom Validations" recipe from Advanced Rails > > Recipes. As far as I can tell, I''ve followed the instructions exactly > > but for some reason this isn''t working and it''s driving me crazy. > > I''ve tried making the methods in the custom validations module class > > methods (self.validates...) as well as the way they are below (which > > is the way they are in the instructions). > > > Any suggestions? > > Have you tried requiring it from an initializer, not the bottom of > environment.rb ? > > Fred > > > Thanks! > > Gavin > > > *** > > lib/custom_validations.rb > > > module CustomValidations > > def validates_phone(*attr_names) > > attr_names.each do |attr_name| > > validates_format_of attr_name, > > :with => /^[\(\)0-9\- \+\.]{10,20} *[extension\.]{0,9} * > > [0-9]{0,5}$/i, > > :message => "invalid" > > end > > end > > def validates_email(*attr_names) > > attr_names.each do |attr_name| > > validates_format_of attr_name, > > :with => /(^([^@\s]+)@((?:[-_a-z0-9]+\.)+[a-z]{2,})$)|(^$)/ > > i, > > :message => "invalid" > > end > > end > > end > > ActiveRecord::Base.extend(CustomValidations) > > > *** > > config/environment.rb > > > # Be sure to restart your server when you modify this file > > > # Uncomment below to force Rails into production mode when > > # you don''t control web/app server and can''t set it the proper way > > ENV[''RAILS_ENV''] ||= ''production'' > > > #normal environment.rb stuff > > > end > > > require ''custom_validations'' > > > *** > > models/user.rb > > > class User < ActiveRecord::Base > > validates_email :email > > end > > > *** > > error from console when starting server > > > /vendor/rails/activerecord/lib/active_record/base.rb:1667:in > > `method_missing'': undefined method `validates_email'' for #<Class: > > 0x31ac9c8> (NoMethodError)--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-09 19:40 UTC
Re: Problem extending ActiveRecord with custom validations
On Dec 9, 7:17 pm, gaveeno <gavin.to...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Fred, that worked! I created config/initializers/ > custom_validations.rb and moved the require line there from the > environment.rb file. It''s not clear to me why this works, and itShort version: because it means the require will happen at the right time. Long version: http://www.spacevatican.org/2008/11/21/environment-rb-and-requiring-dependencies Fred> seems like it''s overkill to create a new file for this, but as long as > it works it''s cool with me. > > Thanks again. > -Gavin > > On Dec 9, 11:09 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On 9 Dec 2008, at 19:05, gaveeno wrote: > > > > I''m having a problem extending ActiveRecord with custom validations > > > based on the "Write Custom Validations" recipe from Advanced Rails > > > Recipes. As far as I can tell, I''ve followed the instructions exactly > > > but for some reason this isn''t working and it''s driving me crazy. > > > I''ve tried making the methods in the custom validations module class > > > methods (self.validates...) as well as the way they are below (which > > > is the way they are in the instructions). > > > > Any suggestions? > > > Have you tried requiring it from an initializer, not the bottom of > > environment.rb ? > > > Fred > > > > Thanks! > > > Gavin > > > > *** > > > lib/custom_validations.rb > > > > module CustomValidations > > > def validates_phone(*attr_names) > > > attr_names.each do |attr_name| > > > validates_format_of attr_name, > > > :with => /^[\(\)0-9\- \+\.]{10,20} *[extension\.]{0,9} * > > > [0-9]{0,5}$/i, > > > :message => "invalid" > > > end > > > end > > > def validates_email(*attr_names) > > > attr_names.each do |attr_name| > > > validates_format_of attr_name, > > > :with => /(^([^@\s]+)@((?:[-_a-z0-9]+\.)+[a-z]{2,})$)|(^$)/ > > > i, > > > :message => "invalid" > > > end > > > end > > > end > > > ActiveRecord::Base.extend(CustomValidations) > > > > *** > > > config/environment.rb > > > > # Be sure to restart your server when you modify this file > > > > # Uncomment below to force Rails into production mode when > > > # you don''t control web/app server and can''t set it the proper way > > > ENV[''RAILS_ENV''] ||= ''production'' > > > > #normal environment.rb stuff > > > > end > > > > require ''custom_validations'' > > > > *** > > > models/user.rb > > > > class User < ActiveRecord::Base > > > validates_email :email > > > end > > > > *** > > > error from console when starting server > > > > /vendor/rails/activerecord/lib/active_record/base.rb:1667:in > > > `method_missing'': undefined method `validates_email'' for #<Class: > > > 0x31ac9c8> (NoMethodError)--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Fred, interesting blog post! On Dec 9, 11:40 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 9, 7:17 pm, gaveeno <gavin.to...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Thanks Fred, that worked! I created config/initializers/ > > custom_validations.rb and moved the require line there from the > > environment.rb file. It''s not clear to me why this works, and it > > Short version: because it means the require will happen at the right > time. > Long version:http://www.spacevatican.org/2008/11/21/environment-rb-and-requiring-d... > > Fred > > > seems like it''s overkill to create a new file for this, but as long as > > it works it''s cool with me. > > > Thanks again. > > -Gavin > > > On Dec 9, 11:09 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > On 9 Dec 2008, at 19:05, gaveeno wrote: > > > > > I''m having a problem extending ActiveRecord with custom validations > > > > based on the "Write Custom Validations" recipe from Advanced Rails > > > > Recipes. As far as I can tell, I''ve followed the instructions exactly > > > > but for some reason this isn''t working and it''s driving me crazy. > > > > I''ve tried making the methods in the custom validations module class > > > > methods (self.validates...) as well as the way they are below (which > > > > is the way they are in the instructions). > > > > > Any suggestions? > > > > Have you tried requiring it from an initializer, not the bottom of > > > environment.rb ? > > > > Fred > > > > > Thanks! > > > > Gavin > > > > > *** > > > > lib/custom_validations.rb > > > > > module CustomValidations > > > > def validates_phone(*attr_names) > > > > attr_names.each do |attr_name| > > > > validates_format_of attr_name, > > > > :with => /^[\(\)0-9\- \+\.]{10,20} *[extension\.]{0,9} * > > > > [0-9]{0,5}$/i, > > > > :message => "invalid" > > > > end > > > > end > > > > def validates_email(*attr_names) > > > > attr_names.each do |attr_name| > > > > validates_format_of attr_name, > > > > :with => /(^([^@\s]+)@((?:[-_a-z0-9]+\.)+[a-z]{2,})$)|(^$)/ > > > > i, > > > > :message => "invalid" > > > > end > > > > end > > > > end > > > > ActiveRecord::Base.extend(CustomValidations) > > > > > *** > > > > config/environment.rb > > > > > # Be sure to restart your server when you modify this file > > > > > # Uncomment below to force Rails into production mode when > > > > # you don''t control web/app server and can''t set it the proper way > > > > ENV[''RAILS_ENV''] ||= ''production'' > > > > > #normal environment.rb stuff > > > > > end > > > > > require ''custom_validations'' > > > > > *** > > > > models/user.rb > > > > > class User < ActiveRecord::Base > > > > validates_email :email > > > > end > > > > > *** > > > > error from console when starting server > > > > > /vendor/rails/activerecord/lib/active_record/base.rb:1667:in > > > > `method_missing'': undefined method `validates_email'' for #<Class: > > > > 0x31ac9c8> (NoMethodError)--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---