I'm using find_by_sql to execute an SQL query.
I would like to be able to use Soundex and Levenshtein, but in order to use Levenshtein I need to include the function as a file.
This is my code so far:
info = params[:email].split('@')
name = info[0]
domain = info[1]
levenshtein = File.open("./lib/assets/mysql-function-levenshtein.sql")
results = Domain.find_by_sql(
"" + levenshtein + "
SELECT *
FROM domains
WHERE domain = '" + domain + "'"
)
I have no idea if simply including it in the query is even valid.
What's the best implementation?
By the way the file I'm trying to include is this: https://github.com/vyper/levenshtein-sql
File.readinstead ofFile.open, or wrap thefind_by_sqlcall in aFile.open do ... endblock. Otherwise I'm not overly fond of using custom SQL queries. Couldn't you just define the function on the database, and do awhere("levensthtein('leonardo', 'leonardu')")? There's no real need to define the function every time you run the query, right?File.readis the one to use.