kalahari
kalahari

Reputation: 974

rails 3 partial search

I made a search (filtering) form to filter my objects according to given values. There is a Company model and search will be according to its attributes. This is my index.html.erb:

<% provide(:title, 'All companies') %>
<h1>All companies</h1>

<%= form_tag companies_path, :method => 'get' do %>
  <%= hidden_field_tag :direction, params[:direction] %>
  <%= hidden_field_tag :sort, params[:sort] %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
<% end %>

<table class="pretty" border="1" cellpadding="10">  
  <tr>  
    <th><%= sortable "name" %></th>
    <th><%= sortable "city" %></th>
    <th><%= sortable "country" %></th>
    <th><%= sortable "street_address" %></th>
    <th><%= sortable "sector" %></th>
    <th><%= sortable "telephone" %></th>
    <th><%= sortable "fax" %></th>
    <th>DELETE</th>
  </tr>  

  <% for company in @companies %>  
  <tr class="<%= cycle('oddrow', 'evenrow') -%>">  
    <td><%= link_to company.name, company %></td>
    <td><%= company.city %></td>
    <td><%= company.country %></td>
    <td><%= company.street_address %></td>
    <td><%= company.sector %></td>
    <td><%= company.telephone %></td>
    <td><%= company.fax %></td>
    <td><% if current_user.admin?  %>
          || <%= link_to "delete", company, method: :delete,
                              data: { confirm: "You sure?" } %>
        <% end %></td>
  </tr>
  <% end %>
</table>
<%= will_paginate @companies %>

This is my companies_controller.rb

helper_method :sort_column, :sort_direction
def index
    @companies = Company.search(params[:search]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 10, :page => params[:page])
end

This is my model company.rb

class Company < ActiveRecord::Base
  attr_accessible :city, :country, :fax, :name, :reseller, :sector, :street_address, :telephone, :id
  has_many :users , dependent: :destroy

  def name_for_form
    "#{name}"
  end

  def self.search(search)
    if search
      q = "%#{search}"
      where('name LIKE ? OR city LIKE ? OR country LIKE ? OR street_address LIKE ? OR telephone LIKE ? OR fax LIKE ? OR sector LIKE ?',
            q,q,q,q,q,q,q)
    else
      scoped
    end
  end

  validates :city, presence: true
  validates :country, presence: true
  validates :fax, presence: true
  validates :name, presence: true
  validates :sector, presence: true
  validates :street_address, presence: true
  validates :telephone, presence: true
end

Lets assume I have 3 companies named kalahari, kalahari 2, and kalahari2. When I search kalahari, it founds only 1 company, kalahari. I mean it can't find kalahari in kalahari 2, or kalahari2. Only founds exact matches. When I search kala, it founds nothing. How can I fix that most simply? I am new to rails and don't want to mess a lot of things.

Upvotes: 0

Views: 282

Answers (1)

MrTheWalrus
MrTheWalrus

Reputation: 9700

The simplest change that will get what you want is adding a wildcard to the end of your search query:

q = "%#{search}%"

The % matches anything when used with LIKE, so your code as currently written will match anything that ends with your input (so it would match a query of 'foo' to 'afoo', 'b_foo', and '1 3 5 x foo'), but without a matching wildcard on the end, it will not match things that contain the query but don't end with it (so 'foo' will not match 'foobar' or 'afoox').

Upvotes: 2

Related Questions