的find_by_sql在Rails 3的阵列格式 [英] find_by_sql with array format in Rails 3

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

问题描述

好日子家伙!

我用的find_by_sql()在轨道3获取记录如下:

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

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

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