搜索句子
我有一个rails应用程序,并已决定进行搜索。 我有一个搜索文本域,控制器负责一切。 在我的搜索方法中
def self.search(search) if search str = [] search.split.each do |s| a = find(:all, :conditions => ['title or content LIKE ?', "%#{s}%" ]) str = (str + a).uniq end else find(:all) end end
我希望能够处理多个单词搜索。 如果删除每个…循环,它可以正常使用1个单词搜索。 无论如何,我想找到每个被搜索单词的相关post并返回其组合。
防爆。 如果有人搜索“最伟大的玩家”,它将返回标题或内容为“最大”的所有post实例以及任何具有标题或内容“播放器”的post
您最有可能应该查看全文搜索:
- 使用Rails进行全文搜索
- http://en.wikipedia.org/wiki/Full_text_search
不应该
a = find(:all, :conditions => ['title or content LIKE ?', "%#{s}%" ])
您只需要将find语句更改为Mischa的代码并添加一个return语句
工作守则:
def self.search(search) if search str = [] search.split.each do |s| a = where(title LIKE ? or content LIKE ?', "%#{s}%", "%#{s}%") str = (str + a).uniq end return str else find(:all) end end