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
First off, I think you’d be better off just defining that function in your database with a migration, so you wouldn’t have to define it again for every query where you want to use it:
With this done, you could also add a scope to your
Domainmodel for doing these kind of queries:With this, you should be able to write your queries something like this: