0

Person has many addresses. I'm trying to create an address for a person if the JSON response from an API is different from any of the addresses in the db.

I wrote the following, which works with literal use cases; it prevents duplicate addresses:

p = Person.find(1)
unless p.addresses.any? { |i| i.address_1.eql?(@response['addresses'][0]['street'].upcase) }

This works by taking the array, mapping the address object, and calling eql? on the address_ column (street address).

However, if the street address is suffixed with "blvd" or "ln", I want to add that address to the person. I haven't found a method that evaluates two strings for similarities. Is there some method like similar_to? that might help me?

sawa
  • 165,429
  • 45
  • 277
  • 381
nulltek
  • 3,247
  • 9
  • 44
  • 94
  • 2
    might help: http://stackoverflow.com/questions/2531502/detect-similar-sounding-words-in-ruby – Doon Oct 06 '15 at 20:00
  • "if ... suffixed ..., I want to add that address"--Do you mean "I do not want to add that address"? – sawa Oct 06 '15 at 20:15
  • You can try to compare strings using the match function [http://ruby-doc.org/core-2.2.0/String.html#method-i-match] – LukeS Oct 06 '15 at 20:45
  • also another option might be to geocode the address an normalize it, then just store the normalized address(es) in the DB. This help with different names(Boulevard vs Blvd, Road vs Rd, etc...) – Doon Oct 06 '15 at 23:46
  • @Doon We actually geocode and normalize the address after it's fed into the Ruby method. The main issue I'm looking at is not importing the same address if it already exists in the database, ergo why I was calling eql? to compare the address from one in the array. – nulltek Oct 07 '15 at 00:17
  • gotcha. how close do you want to compare, what is simliar enough? what 'starts_with?' or maybe 'include?' to test. – Doon Oct 07 '15 at 01:39
  • @Doon to give you an example. If I ran my method and the persisted address was "111 West 8th St Houston, TX 77057" and the inbound address was "112 West 8th St Houston, TX 77057" then I would definitely want to create a new address based off of the inbound since the street number changed. Really the goal is to look for change of addresses and populate, small changes like normalization of Street into St is not as important. – nulltek Oct 07 '15 at 12:39

0 Answers0