Skip to content

Instantly share code, notes, and snippets.

@moneill
Created May 4, 2012 21:55
Show Gist options
  • Save moneill/2597984 to your computer and use it in GitHub Desktop.
Save moneill/2597984 to your computer and use it in GitHub Desktop.
Soundex test
irb(main):016:0> Person.where("soundex(first_name) LIKE soundex(?)", "%trinitya%")
Person Load (16.9ms) SELECT `people`.* FROM `people` WHERE (soundex(first_name) LIKE soundex('%trinitya%'))
=> [#<Person id: 5, first_name: "Trinity", middle_name: nil, last_name: "Kuvalis", age: 22, email: "samir_wilderman@oconner.com", phone: "509-938-1572 x4260", address_1: "28790 Minerva Highway", address_2: nil, state: "PW", zip: 66274, contact_preference: nil, comments: "Natus porro aut itaque placeat sunt. Odio quas omni...", sources_of_interest: nil, other_sources_of_interest: nil, mailing_subscriptions: nil, lumbar_puncture: nil, study_partner: nil, study_partner_relationship: nil, adc_comments: "Sed porro blanditiis nemo. Sed facilis ea. Ea quis ...", created_at: "2012-05-04 19:53:05", updated_at: "2012-05-04 19:53:05", city: "East Clarissaburgh", active: true>]
irb(main):017:0> Person.where("soundex(first_name) LIKE soundex(?)", "%trinityaa%")
Person Load (0.4ms) SELECT `people`.* FROM `people` WHERE (soundex(first_name) LIKE soundex('%trinityaa%'))
=> [#<Person id: 5, first_name: "Trinity", middle_name: nil, last_name: "Kuvalis", age: 22, email: "samir_wilderman@oconner.com", phone: "509-938-1572 x4260", address_1: "28790 Minerva Highway", address_2: nil, state: "PW", zip: 66274, contact_preference: nil, comments: "Natus porro aut itaque placeat sunt. Odio quas omni...", sources_of_interest: nil, other_sources_of_interest: nil, mailing_subscriptions: nil, lumbar_puncture: nil, study_partner: nil, study_partner_relationship: nil, adc_comments: "Sed porro blanditiis nemo. Sed facilis ea. Ea quis ...", created_at: "2012-05-04 19:53:05", updated_at: "2012-05-04 19:53:05", city: "East Clarissaburgh", active: true>]
@moneill
Copy link
Author

moneill commented May 4, 2012

Here's a way to find "nearby" (in terms of word sound) records. It won't be perfect, but as above, you can luck out and get matches if the search term sounds close to a column value.

@moneill
Copy link
Author

moneill commented May 4, 2012

(In case above "trinityg" wouldn't match, but "trinitya" would)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment