Rails像完整单词中的返回匹配需要完全匹配

时间:2015-05-27 06:03:44

标签: ruby-on-rails

所以现在当我搜索“cook”时,我的搜索会返回cookie。

我需要它返回“cook”然后如果什么都不存在则返回“cookies”

class SearchesController < ApplicationController
  def search
    @sentence = Sentence.where("LOWER(verse) LIKE LOWER(?)", "%" + params[:search_term] + "%")
    respond_to do |format|
      format.html
  end
end

2 个答案:

答案 0 :(得分:1)

你可以尝试这个,但可能不是有效的,因为它会进行两次查询:

@sentence = Sentence.where("LOWER(verse) LIKE LOWER(?)", params[:search_term])
@sentence_alternatives = Sentence.where("LOWER(verse) LIKE LOWER(?)", "%#{params[:search_term]}%") if @sentence.blank?

另一种方法是将orwhere

一起使用
@sentence = Sentence.where("LOWER(verse) LIKE LOWER(?)", params[:search_term]).or.where("LOWER(verse) LIKE LOWER(?)", "%#{params[:search_term]}%")

答案 1 :(得分:0)

@sentence = Sentence.where("LOWER(verse) LIKE LOWER(%?%)", params[:search_term])