区分大小写的搜索限制了结果

时间:2015-03-21 23:02:30

标签: ruby-on-rails ruby search

我正在尝试修改我的搜索,这样如果用户搜索“争吵者”,他们将获得与搜索“牧马人”相同的结果。数据库的第一个字母大写,所以我想如果小写,我需要提高我的搜索量。不知道如何实现这一点 - 基本上,我不希望大写在搜索时完全发挥作用。

我的搜索表单:

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :heading %><br>
    <%= f.text_field :heading %>
  </div>
  <div class="field">
    <%= f.label :body %><br>
    <%= f.text_area :body %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.text_field :price %>
  </div>
  <!--<div class="field">
    <%= f.label :neighborhood %><br>
    <%= f.text_field :neighborhood %>
  </div> -->
  <div class="field">
    <%= f.label :external_url %><br>
    <%= f.text_field :external_url %>
  </div>
  <div class="field">
    <%= f.label :timestamp %><br>
    <%= f.text_field :timestamp %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我的帖子控制器:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

def home
end

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.order('timestamp DESC').paginate(:page => params[:page], :per_page => 50)
    @posts = @posts.where(model: params["model"]) if params["model"].present?
    @posts = @posts.where(year: params["year"]) if params["year"].present?
    @posts = @posts.where(neighborhood: params["neighborhood"]) if params["neighborhood"].present?
    @posts = @posts.where("price > ?", params["min_price"]) if params["min_price"].present?
    @posts = @posts.where("price < ?", params["max_price"]) if params["max_price"].present?
    @posts = @posts.where(transmission: params["transmission"]) if params["transmission"].present?
    @posts = @posts.where(title_status: params["title_status"]) if params["title_status"].present?
  end

  # GET /posts/

2 个答案:

答案 0 :(得分:1)

这是一个小例子。

@posts = @posts.where( all, :conditions => ["lower(model) =?", params["model"].downcase]) if params["model"].present?

此处发生的事情 lower(field_name)会使字段值小写

params [:name] .downcase 使值小写

答案 1 :(得分:1)

@posts = @ posts.where(all,:conditions =&gt; [“lower(model)=?”,params [“model”]。downcase])如果params [“model”]。present?