Hey I''m running into an small issue with searching a zip codes table, where some zip codes contain 1 or more leading zeros. This problem could be fixed with some database level hacking -- but I''d rather attack the problem in rails. Zip codes are stored in the zip code table with leading zeros dropped. 01234 becomes 1234. Thus, when someone searches via the website for 012345, it will not find any results. I''m looking to write a method (probably in the Zipcode model) to remove any leading zeros from an incoming search request, before it gets passed into sql calls. I looked into callbacks, but those appears to only be useful when creating/saving/updating/deleting data, whereas I need to address searching. Any suggestions on the correct/best way to do this? Again, I''m looking for a rails solution, not a database level solution. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
reg exp On Sun, Jan 3, 2010 at 3:02 PM, jsoo <wwwjscom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey > > I''m running into an small issue with searching a zip codes table, > where some zip codes contain 1 or more leading zeros. This problem > could be fixed with some database level hacking -- but I''d rather > attack the problem in rails. > > Zip codes are stored in the zip code table with leading zeros dropped. > 01234 becomes 1234. Thus, when someone searches via the website for > 012345, it will not find any results. > > I''m looking to write a method (probably in the Zipcode model) to > remove any leading zeros from an incoming search request, before it > gets passed into sql calls. > > I looked into callbacks, but those appears to only be useful when > creating/saving/updating/deleting data, whereas I need to address > searching. > > Any suggestions on the correct/best way to do this? Again, I''m > looking for a rails solution, not a database level solution. > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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. > > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Regex fails on validation even. Plus just saying reg exp is not very helpful. I''m not sure how to trap the call before it reaching sql (assuming that is the best way to do it) which is really what I''m trying to figure out. Sidebar: validates_format_of :zip_code, :with => /\A[0-9]{5}\Z/i, fails on something like this 03062. --- Jason Soo "I''m going to live forever or die trying." On Sun, Jan 3, 2010 at 2:32 PM, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> reg exp > > On Sun, Jan 3, 2010 at 3:02 PM, jsoo <wwwjscom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hey > > > > I''m running into an small issue with searching a zip codes table, > > where some zip codes contain 1 or more leading zeros. This problem > > could be fixed with some database level hacking -- but I''d rather > > attack the problem in rails. > > > > Zip codes are stored in the zip code table with leading zeros dropped. > > 01234 becomes 1234. Thus, when someone searches via the website for > > 012345, it will not find any results. > > > > I''m looking to write a method (probably in the Zipcode model) to > > remove any leading zeros from an incoming search request, before it > > gets passed into sql calls. > > > > I looked into callbacks, but those appears to only be useful when > > creating/saving/updating/deleting data, whereas I need to address > > searching. > > > > Any suggestions on the correct/best way to do this? Again, I''m > > looking for a rails solution, not a database level solution. > > > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 3, 2010, at 12:02 PM, jsoo wrote:> Hey > > I''m running into an small issue with searching a zip codes table, > where some zip codes contain 1 or more leading zeros. This problem > could be fixed with some database level hacking -- but I''d rather > attack the problem in rails. > > Zip codes are stored in the zip code table with leading zeros dropped. > 01234 becomes 1234. Thus, when someone searches via the website for > 012345, it will not find any results. > > I''m looking to write a method (probably in the Zipcode model) to > remove any leading zeros from an incoming search request, before it > gets passed into sql calls. > > I looked into callbacks, but those appears to only be useful when > creating/saving/updating/deleting data, whereas I need to address > searching. > > Any suggestions on the correct/best way to do this? Again, I''m > looking for a rails solution, not a database level solution.Maybe not ideal, but you could always just call to_i on the parameter that gets passed into your find method. That should strip it. -philip -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Curtis Jennings Schofield
2010-Jan-03 22:18 UTC
Re: Zip Code/Leading Zero''s search problem
be more explicit in your reg exp ie the first numeric must be [1-9] On Sun, Jan 3, 2010 at 1:36 PM, Jason Soo <wwwjscom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Regex fails on validation even. > Plus just saying reg exp is not very helpful. I''m not sure how to trap the > call before it reaching sql (assuming that is the best way to do it) which > is really what I''m trying to figure out. > > > Sidebar: > validates_format_of :zip_code, :with => /\A[0-9]{5}\Z/i, > fails on something like this 03062. > --- > Jason Soo > "I''m going to live forever or die trying." > > > On Sun, Jan 3, 2010 at 2:32 PM, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> reg exp >> >> On Sun, Jan 3, 2010 at 3:02 PM, jsoo <wwwjscom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > Hey >> > >> > I''m running into an small issue with searching a zip codes table, >> > where some zip codes contain 1 or more leading zeros. This problem >> > could be fixed with some database level hacking -- but I''d rather >> > attack the problem in rails. >> > >> > Zip codes are stored in the zip code table with leading zeros dropped. >> > 01234 becomes 1234. Thus, when someone searches via the website for >> > 012345, it will not find any results. >> > >> > I''m looking to write a method (probably in the Zipcode model) to >> > remove any leading zeros from an incoming search request, before it >> > gets passed into sql calls. >> > >> > I looked into callbacks, but those appears to only be useful when >> > creating/saving/updating/deleting data, whereas I need to address >> > searching. >> > >> > Any suggestions on the correct/best way to do this? Again, I''m >> > looking for a rails solution, not a database level solution. >> > >> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > For more options, visit this group at >> > http://groups.google.com/group/rubyonrails-talk?hl=en. >> > >> > >> > >> >> -- >> >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
I ended up solving the problem using Philip''s solution (thanks). Curtis, thanks for the recommendation, but zero is a valid leading value for some zip codes, so only doing a range of 1-9 wouldn''t be as tight of a check as I would''ve liked. Thank you all for your help and replies. --- Jason Soo "I''m going to live forever or die trying." On Sun, Jan 3, 2010 at 4:18 PM, Curtis Jennings Schofield < curtis.schofield-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> be more explicit in your reg exp > ie the first numeric must be [1-9] > > > On Sun, Jan 3, 2010 at 1:36 PM, Jason Soo <wwwjscom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Regex fails on validation even. > > Plus just saying reg exp is not very helpful. I''m not sure how to trap > the > > call before it reaching sql (assuming that is the best way to do it) > which > > is really what I''m trying to figure out. > > > > > > Sidebar: > > validates_format_of :zip_code, :with => /\A[0-9]{5}\Z/i, > > fails on something like this 03062. > > --- > > Jason Soo > > "I''m going to live forever or die trying." > > > > > > On Sun, Jan 3, 2010 at 2:32 PM, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> > >> reg exp > >> > >> On Sun, Jan 3, 2010 at 3:02 PM, jsoo <wwwjscom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> > Hey > >> > > >> > I''m running into an small issue with searching a zip codes table, > >> > where some zip codes contain 1 or more leading zeros. This problem > >> > could be fixed with some database level hacking -- but I''d rather > >> > attack the problem in rails. > >> > > >> > Zip codes are stored in the zip code table with leading zeros dropped. > >> > 01234 becomes 1234. Thus, when someone searches via the website for > >> > 012345, it will not find any results. > >> > > >> > I''m looking to write a method (probably in the Zipcode model) to > >> > remove any leading zeros from an incoming search request, before it > >> > gets passed into sql calls. > >> > > >> > I looked into callbacks, but those appears to only be useful when > >> > creating/saving/updating/deleting data, whereas I need to address > >> > searching. > >> > > >> > Any suggestions on the correct/best way to do this? Again, I''m > >> > looking for a rails solution, not a database level solution. > >> > > >> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> > To unsubscribe from this group, send email to > >> > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > >> > For more options, visit this group at > >> > http://groups.google.com/group/rubyonrails-talk?hl=en. > >> > > >> > > >> > > >> > >> -- > >> > >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> To unsubscribe from this group, send email to > >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > >> For more options, visit this group at > >> http://groups.google.com/group/rubyonrails-talk?hl=en. > >> > >> > > > > -- > > > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > > For more options, visit this group at > > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Curtis Jennings Schofield
2010-Jan-03 23:39 UTC
Re: Zip Code/Leading Zero''s search problem
Ahh Jason - Thank you - If i understand your problem in correctly - you wanted to turn the value into the database into a non-zipcode format to allow for people to search without including the leading zero? Also - thank you for the information about zero being a leading diget for zip codes. On Sun, Jan 3, 2010 at 3:22 PM, Jason Soo <wwwjscom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I ended up solving the problem using Philip''s solution (thanks). > Curtis, thanks for the recommendation, but zero is a valid leading value for > some zip codes, so only doing a range of 1-9 wouldn''t be as tight of a check > as I would''ve liked. > Thank you all for your help and replies. > --- > Jason Soo > "I''m going to live forever or die trying." > > > On Sun, Jan 3, 2010 at 4:18 PM, Curtis Jennings Schofield > <curtis.schofield-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> be more explicit in your reg exp >> ie the first numeric must be [1-9] >> >> >> On Sun, Jan 3, 2010 at 1:36 PM, Jason Soo <wwwjscom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > Regex fails on validation even. >> > Plus just saying reg exp is not very helpful. I''m not sure how to trap >> > the >> > call before it reaching sql (assuming that is the best way to do it) >> > which >> > is really what I''m trying to figure out. >> > >> > >> > Sidebar: >> > validates_format_of :zip_code, :with => /\A[0-9]{5}\Z/i, >> > fails on something like this 03062. >> > --- >> > Jason Soo >> > "I''m going to live forever or die trying." >> > >> > >> > On Sun, Jan 3, 2010 at 2:32 PM, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >> >> reg exp >> >> >> >> On Sun, Jan 3, 2010 at 3:02 PM, jsoo <wwwjscom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> > Hey >> >> > >> >> > I''m running into an small issue with searching a zip codes table, >> >> > where some zip codes contain 1 or more leading zeros. This problem >> >> > could be fixed with some database level hacking -- but I''d rather >> >> > attack the problem in rails. >> >> > >> >> > Zip codes are stored in the zip code table with leading zeros >> >> > dropped. >> >> > 01234 becomes 1234. Thus, when someone searches via the website for >> >> > 012345, it will not find any results. >> >> > >> >> > I''m looking to write a method (probably in the Zipcode model) to >> >> > remove any leading zeros from an incoming search request, before it >> >> > gets passed into sql calls. >> >> > >> >> > I looked into callbacks, but those appears to only be useful when >> >> > creating/saving/updating/deleting data, whereas I need to address >> >> > searching. >> >> > >> >> > Any suggestions on the correct/best way to do this? Again, I''m >> >> > looking for a rails solution, not a database level solution. >> >> > >> >> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> >> > To unsubscribe from this group, send email to >> >> > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> >> > For more options, visit this group at >> >> > http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > >> >> > >> >> > >> >> >> >> -- >> >> >> >> 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. >> >> To unsubscribe from this group, send email to >> >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> >> For more options, visit this group at >> >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >> >> >> > >> > -- >> > >> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > For more options, visit this group at >> > http://groups.google.com/group/rubyonrails-talk?hl=en. >> > >> >> -- >> >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.