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