开发者

Why is my in (?) clause running the sql id in ('1,2,3') instead of id in (1,2,3)

I have a string

"1,2,3"

in a varialbe, I am doing:

User.where("id in (?)", user_id_list)

resulting sql is:

select users.* from users where (id in ('1,3,4'))

I want:

select users.* from users where (id in (1,3,4))

How开发者_如何学C to fix this?

does user_id_list have to be an array?


When you pass in a string value, rails will wrap it with quotes, which is why you see your results.

When you have an array, you can just do:

User.where(:id => user_id_list)

and Rails will automatically create the IN clause for you.

Since those are user id's with integers, I would suggest just doing:

User.where(:id => user_id_list.split(',').map(&:to_i))
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜