是否有一种 DRY 方法可以将绑定变量传递给 AR 关系? [英] Is there a DRY way to pass bind variables to an AR Relation?
本文介绍了是否有一种 DRY 方法可以将绑定变量传递给 AR 关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码:
t = "%#{term}%"
where('name like ? or email like ? or postcode like ?', t, t, t)
如您所见,它在多个字段中执行搜索.
As you can see it's performing a search across several fields.
有没有办法避免重复的t?这让我觉得很脏.
Is there a way to avoid the duplicated t? It's making me feel dirty.
谢谢
推荐答案
您可以使用命名占位符来实现:
You can do it with a named placeholder:
where('name LIKE :name OR email LIKE :name OR postcode LIKE :name', :name => t)
这通常是在查询中多次重复奇异值的最佳方式.
This is often the best way to repeat a singular value several times in a query.
这篇关于是否有一种 DRY 方法可以将绑定变量传递给 AR 关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文