Hey guys! First time posting so let see how this goes.. got a question: I''m trying to find a record in a table minus any leading / trailing whitespaces. This code will find the record but it wont return anything if the value in the table''s column (named MyColumn) had any whitespaces at the beginning or at the end of it: found_record = MyTable.find(:first,:conditions => [ "MyColumn = ?", someValue ]) I tried this and it obviously didnt work: found_record = MyTable.find(:first,:conditions => [ "MyColumn.strip ?", someValue ]) I dont wanna modify the data in the table, just find a match. What can I do? Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 4 Dec 2007, at 20:37, Nabeel Mr. wrote:> > Hey guys! First time posting so let see how this goes.. got a > question: > > I''m trying to find a record in a table minus any leading / trailing > whitespaces. > > This code will find the record but it wont return anything if the > value in the table''s column (named MyColumn) had any whitespaces at > the beginning or at the end of it: > found_record = MyTable.find(:first,:conditions => [ "MyColumn = ?", > someValue ]) > > I tried this and it obviously didnt work: > found_record = MyTable.find(:first,:conditions => [ "MyColumn.strip > ?", someValue ]) >At this point you''ve got 2 solutions: - at the point of saving the record, strip the whitespace. The whole problem goes away completely (that''s what I''d do). - drop down to some slightly rawer sql. Assuming your database has a function that removes trailing and leading whitespace (in mysql it''s TRIM) you can do something like MyTable.find(:first,:conditions => [ "TRIM(MyColumn) = ?",someValue ]) Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The :conditions option gets plugged into a sql statement, so you can use whatever facilities your database provides for that sort of thing. In PostgreSQL, I''d do something like: :conditions => ["trim(both '' '' from MyColumn) = ?", someValue] Peace, Phillip On Dec 4, 2007, at 2:37 PM, Nabeel Mr. wrote:> > Hey guys! First time posting so let see how this goes.. got a > question: > > I''m trying to find a record in a table minus any leading / trailing > whitespaces. > > This code will find the record but it wont return anything if the > value in the table''s column (named MyColumn) had any whitespaces at > the beginning or at the end of it: > found_record = MyTable.find(:first,:conditions => [ "MyColumn = ?", > someValue ]) > > I tried this and it obviously didnt work: > found_record = MyTable.find(:first,:conditions => [ "MyColumn.strip > ?", someValue ]) > > I dont wanna modify the data in the table, just find a match. > What can I do? Thanks! > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 guys! The TRIM SQL statement worked like a charm :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---