在 Rails 3 中使用数组格式的 find_by_sql [英] find_by_sql with array format in Rails 3

查看:16
本文介绍了在 Rails 3 中使用数组格式的 find_by_sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我在 rails 3 中使用 find_by_sql() 来获取记录,如下所示.

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]])

这里,sent_idfrom_id 属性都接收相同的参数 params[:id]

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

那么,不是两次传递相同的params[:id],是否有任何机制可以根据顺序传递参数?

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

推荐答案

您可以使用散列来命名插值,如下所示:

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]}])

这篇关于在 Rails 3 中使用数组格式的 find_by_sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆