开发者

Can't figure out the syntax for this AR query

开发者 https://www.devze.com 2023-03-18 01:59 出处:网络
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

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?
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号