DO"像"用ActiveRecord查询中的Rails 2.x和3.x? [英] Do "like" queries with ActiveRecord in Rails 2.x and 3.x?

查看:99
本文介绍了DO"像"用ActiveRecord查询中的Rails 2.x和3.x?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做这样的查询中的Rails 3.x中

I'm doing queries like this in Rails 3.x

Speaker.where("name like '%yson%'")

但我很想避免DB特定code。什么是正确的方式做到这一点?

but I'd love to avoid the DB specific code. What's the right way to do this?

如果有办法做到这一点的Rails 2.X太多,这将有助于太多。

If there's a way to do this in Rails 2.x too, that would help too.

推荐答案

您可以使用.matches吧。

You can use .matches for it.

 > t[:name].matches('%lore').to_sql
 => "\"products\".\"name\" LIKE '%lore'"

在查询实际使用量是:

Actual usage in a query would be:

Speaker.where(Speaker.arel_table[:name].matches('%lore'))

这篇关于DO"像"用ActiveRecord查询中的Rails 2.x和3.x?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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