您可以使用ActiveRecord找到一个字段的子? (快速和放脏关键字查找器)您可以、字段、关键字、快速

2023-09-08 18:45:22 作者:败给xx

假设一个数据库包含字段'关键词'和采样记录包括: 管扳手 猴子扳手 月牙扳手 新月滚 单杠

Suppose a database contains a field 'keywords' and sample records include: "pipe wrench" "monkey wrench" "crescent wrench" "crescent roll" "monkey bars"

有没有办法在ActiveRecord的寻找到关键字字段包含子记录新月?

is there a way in activerecord to find the records where the keyword field contains the substring "crescent"?

(这只是一个快速和肮脏的查找,快速原型的概念)

(It's just a quick and dirty lookup for a quick concept prototype)

推荐答案

是啊,只使用一个LIKE语句在MySQL中。

Yeah, just use a LIKE statement in MySQL.

在Rails的2.X:

In Rails 2.x:

Table.find(:all, :conditions => ['keywords LIKE ?', '%crescent%'])

在Rails 3.x的:

In Rails 3.x:

Table.where('keywords LIKE ?', '%crescent%').all