I am using the rails3-jquery-autocomplete gem to autocomplete the a field (product) in a form in my rails 3 app. The user can either type in the product name or the product code, which are both string columns in the products table.
Everything works fine locally, but on heroku the ajax request crashes with the standard 500 heroku error:
We're sorry, but something went wrong (500)
I think this might be a postgre / sql issue – I use mysql locally for development but heroku runs on a postgre sql database, and I am doing the following query in the function that gets calles by the ajax autocomplete request:
def get_autocomplete_items(parameters)
items = Product.select("DISTINCT CONCAT_WS(' ', product_code, title, id) AS full_name, product_code, title, id").where(["CONCAT_WS(' ', product_code, title) LIKE ?", "%#{parameters[:term]}%"])
end
Locally, this would return me an array on json format including all matching produc_ids and names:
[{"id":"9","label":"xt-pnt-dress_45 - Catherine Malandrino","value":"xt-pnt-dress_45 - Catherine Malandrino"}, ... ]
If anybody has an idea how to change that query that it conforms with heroku or any others idea I’d be really grateful. thx.
Heroku uses PostgreSQL 8.3 for shared databases and 9.0 for dedicated databases. Neither version 8.3 nor version 9.0 have a
concat_wsfunction, that function is only available in version 9.1+.You can concatenate the strings by hand though:
That will work as long as none of
product_code,title, andidare NULL. If you might have NULLs then you can wrap them COALESCE (e.g.COALESCE(product_code || ' ', '')) to turn them into empty strings.Alternatively, you could take care of the
full_namein Ruby:and have a separate generated column for the LIKE or check both columns with LIKE:
Also, you should be aware that MySQL’s LIKE is case insensitive but PostgreSQL’s is not so you might want to downcase everything to avoid confusion:
Since
||is a logical-OR in MySQL, joining the three strings in Ruby (i.e.def full_name) and checkingproduct_codeandtitlewith separate downcased LIKEs is probably the cleanest portable solution.Switching your development environment to PostgreSQL would also be a good idea, matching versions with your deployment environment would also be a good idea. There are other differences that will cause trouble.