当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby - rails activerecord how to use a hash as a parameter of where method(like condition)

问题描述:

when i use the where method,

users.where('id = ?', params[:id])

then i got what i want.

if i want to do upward statement using hash, i can do this.

users.where(:id => params[:id])

then i can get exactly same things


but now,

users.where('nickname like ?', '%' + params[:nickname] + '%')

then, how can i do exactly same thing using hash as a parameter of where method?

网友答案:

You cannot use Hash syntax in this case. The best way is to do what you are doing right now. But you can shorten the query a little bit using interpolation

where('nickname like ?', "%#{ params[:nickname] }%")
分享给朋友:
您可能感兴趣的文章:
随机阅读: