Can't figure out the syntax for this AR query
I'm trying to开发者_如何学运维 run this query:
Result.where('link = #{site}').present?
where site
is a string. However the problem with the code above is that because of the single quotes, site
will not be interpolated.
If i try
Result.where("link = #{site}").present?
I get a syntax error.
How can I write this?
You will be hacked next day you use it. It has severe SQL injection issue.
What you have to do is this:
Result.where(:link => site)
Or if you need something other than equality then this syntax is better:
Result.where('link <> ?', site)
You can read more here.
If my memory serves I think you can double up the quotes:
Result.where('"link = #{site}"').present?
精彩评论