A collection of computer systems and programming tips that you may find useful.
 
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Wednesday, October 27, 2010

Searchlogic versus MetaSearch and Rails3

Searchlogic is a great Rails gem for basic searching of your tables. I've used it for quite a while now. Problem is that at the time of writing (October 2010) it does not work with Rails3.

You can install it just fine but starting up your server will produce and error like this:
/Users/jones/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.1/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method': undefined method `merge_joins' for class `Class' (NameError)


What to do? Well there is an alternative search gem called MetaSearch that has a very similar interface to Searchlogic and which does work with Rails3. In fact it may be a little better.

In my code I have search code in three places - the index action of my controllers, a search form at the top of my index view pages and column headers on my index view pages that will reorder the matching rows when I click on them.

So here are the three code blocks in Searchlogic and then MetaSearch (with a very simple model where I'm simple searching the name column). Note that I'm using will_paginate in both cases.

Controller Index Action

Searchlogic
  def index
@search = Product.search(params[:search])
@search.order ||= :ascend_by_name
@products = @search.all.paginate :page => params[:page], :per_page => 20
end

MetaSearch
  def index
@search = Product.search(params[:search])
@search.meta_sort ||= 'name.asc'
@products = @search.all.paginate :page => params[:page], :per_page => 20
end

View Index Page Search Form (omitting some of the html formatting)

Searchlogic
<% form_for @search do |f| %>
<p>SEARCH</p>
<p><%= f.label :name_like, 'Name' %>
<%= f.text_field :title_like, :size => 15 %>
<%= f.submit "SEARCH" %>
<%= @search.count %>Matches</p>
<% end %>

MetaSearch - NO CHANGE!

View Index Page Column Headers (just showing a single column header)

Searchlogic
<%= order @search, :by => 'name', :as => 'name'.humanize %>

MetaSearch
<%= sort_link @search, 'name', 'name'.upcase %>

All in all, a pretty straightforward replacement.

But there is at least on additional bonus, over and about working with Rails3. 'attr_searchable' and 'assoc_searchable' allow you to specify in your models exactly which fields can and cannot be searched. Searching in either Searchlogic or MetaSearch uses GET requests which display the search parameters in the URL in the browser. That opens the door to people trying out other likely field names and searching otherwise private data. This mechanism provides a way to limit that problem.

I have no doubt that searchlogic will get updated soon, as so many people use it. But until then MetaSearch is the way to go.


 

4 comments:

NotThatCarl said...

Having just tried to use searchlogic in a Rails 3.0.1 app I'm working on for a client, this article was a life saver! Googling for 10 minutes didn't find anything directly related until I stumbled upon your article, and then BAM! It all works. Saved me tons of additional work and made my users very happy.

Anonymous said...

Note that the rd_searchlogic gem provides searchlogic support for Rails 3.

Unknown said...

Hope you've found MetaSearch to be helpful. I may be biased, but I think there are considerable benefits to sticking with MetaSearch for the long haul. :)

rmagnum2002 said...

Finaly found out how to sort search results by default with metasearch. Thank you and thanks gooogle :) I am using meta_search in rails 3.0 app and I am happy. easy one

Archive of Tips