Hello all, I''m trying to figure out how to validate a zip code in my user model. I can validate it''s all nuneric and five digits but I would like to validate it against a zip code table. I know I need to create a zipcodes table and assume a zipcodes model. In the user model, how/where do I call the Zipcode.find ? Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Mark, I''m assuming you want to validate the zipcode when saving (say) an address. So in your address model, put something like this: validates_each :zipcode do |model, attr, value| model.errors.add(attr, "is not valid") if Zipcode.find_by_zipcode (value).nil? end Where Zipcode is the name of your zipcode model that contains valid zipcodes. This assumes that zipcodes in the zipcode table are unique. I don''t think I''ve ever used the word zipcode this many times in a single email. -Dan On 9/18/07, Mark <mark-h9LRGznmPqMAvxtiuMwx3w@public.gmane.org> wrote:> > > Hello all, > > I''m trying to figure out how to validate a zip code in my user model. > I can validate it''s all nuneric and five digits but I would like to > validate it against a zip code table. > > I know I need to create a zipcodes table and assume a zipcodes model. > In the user model, how/where do I call the Zipcode.find ? > > Thanks > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 Dan. This is just what I was looking for! ----- Original Message ---- From: Dan Falcone <danfalcone-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Sent: Tuesday, September 18, 2007 2:13:18 PM Subject: [Rails] Re: Custom Validation Hi Mark, I''m assuming you want to validate the zipcode when saving (say) an address. So in your address model, put something like this: validates_each :zipcode do |model, attr, value| model.errors.add (attr, "is not valid") if Zipcode.find_by_zipcode(value).nil? end Where Zipcode is the name of your zipcode model that contains valid zipcodes. This assumes that zipcodes in the zipcode table are unique. I don''t think I''ve ever used the word zipcode this many times in a single email. -Dan On 9/18/07, Mark <mark-h9LRGznmPqMAvxtiuMwx3w@public.gmane.org> wrote: Hello all, I''m trying to figure out how to validate a zip code in my user model. I can validate it''s all nuneric and five digits but I would like to validate it against a zip code table. I know I need to create a zipcodes table and assume a zipcodes model. In the user model, how/where do I call the Zipcode.find ? Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---