find_by_sql with array format in Rails 3

◇◆丶佛笑我妖孽 提交于 2019-12-18 15:35:36

问题


good day guys!

I'm using find_by_sql() in rails 3 to fetch records as follows.

@list=Email.find_by_sql(["SELECT * FROM Emails WHERE sent_id=?",params[:id]])

How to modify the same statement if multiple parameter applies for same attribute, say for example :

@list=Email.find_by_sql(["SELECT * FROM Emails WHERE (sent_id=? OR from_id=?)",params[:id],params[:id]])

Here, both sent_id and from_id attributes receives same parameter params[:id]

So, instead of passing same params[:id] twice, is there any mechanism available to pass parameter based on order?


回答1:


You can use a hash to name the interpolated values, like this:

@list = Email.find_by_sql(["SELECT * FROM Emails WHERE (sent_id = :id OR from_id = :id)", {:id => params[:id]}])


来源:https://stackoverflow.com/questions/10977104/find-by-sql-with-array-format-in-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!