remove apostrophe in query


I want to generate a query with conditions like that

:conditions => ["score_team_1 ? score_team_2", compare_sign] .

The resulting query ends e.g. with score_team_1 '=' score_team_2 How can i remove the apostrophes from include into the string? Thanks

You can't really do that like this - bind variables as designed for inserting literals. 'Normal' ruby interpolation might be the way forward here (assuming that compare sign is coming from a trusted source, if not sanitize it first).


You don't need to use the array to populate conditions, as long as the "compare_sign" isn't user-supplied data; ie: you *know* that it isn't going to contain something that will compromise your DB, because you set it earlier.

:conditions => "score_team_1 #{compare_sign} score_team_2"

...but there's probably a *nice* way of doing it.

You could also consider switching the way the condition is defined:

score_team_1 < score_team_2

is equivalent to:

score_team_1 - score_team_2 < 0

which (on MySQL - check your DB for details) is equivalent to:

SIGN(score_team_1 - score_team_2) = -1

The last form is handy, as the number at the end controls whether you end up with < or >.

No idea if the different queries might have different performance - try it and see, if it's an issue.

--Matt Jones