Topic: Dynamic Search tool help

Sorry if this sounds to newb still...

I can't make heads or tails of search for rails .. The crud stuff you learn at the onset of Rails is great!  And then to edit it to do new things even better ... Buuuut ominously search is barely brushed on in the pragmatic rails book.. Worse yet the wiki has outdated posts of search tools that are irksome.. But again I am still very newb..

I need to get a search tool that allows filtering of tables or fields to search in .. Like on default it would search all fields but a simple click and choose would lesson the tables or fields to search in..

I have found examples accross the web but they are very scattered.. Please help.. Is there any simi sure fire search tool anyone has discovered to be invaluable and easy enough to install and configure ?

Last edited by tripdragon (2006-07-07 08:43:49)

Re: Dynamic Search tool help

I have never come across a great way to limit searching based on fields in Rails. I'm not saying it isn't out there, but I haven't seen it. There are a couple of ways you can do it which are probably not the prettiest but should work.

You could build your own SQL queries and run it with find_by_sql(). That should take care of your issue entirely, but you'll need to brush up on your SQL if you quickly forgot it.

The second option, which would proabably still be more work would be to look at using Association Extensions to search subsets of your data. I made a post about this which you can view here:

Most code examples are usually pulled out of the air and not tested. Use at your own risk!

Re: Dynamic Search tool help

Arrgg well that sucks.. thats mainly the reason I swapped to Rails from filemaker

Hpfh.. Well can you help with this then >?

From here … tWithRails

I followed everything and after I load create_index I get

uninitialized constant FerretConfig
RAILS_ROOT: script/../config/..

Application Trace | Framework Trace | Full Trace
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in `const_missing'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in `const_missing'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:133:in `const_missing'
#{RAILS_ROOT}/app/controllers/search_controller.rb:5:in `create_index'
This error occured while loading the following files:

No where in the doc does    ferret_config.rb come up and a search in google does not help

Re: Dynamic Search tool help

I find it best to start with the most obvious questions first. Have you checked that ferret_config.rb exists and you can be executed?

Most code examples are usually pulled out of the air and not tested. Use at your own risk!

Re: Dynamic Search tool help

no there is no ferret_config.rb file .. so I used the ferret_environment.rb  as the ferret_config.rb and it worked .. so I am now here … tWithRails

I followed everything here but at the last I get when loading page

Can only handle a String or a Query.
RAILS_ROOT: script/../config/..

Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/models/result.rb:27:in `search_each'
#{RAILS_ROOT}/app/models/result.rb:27:in `search_index'
#{RAILS_ROOT}/app/models/result.rb:15:in `count'
#{RAILS_ROOT}/app/controllers/search_controller.rb:25:in `get_results'

I'm beat,, I did everything I mostly understand what is happeneing on
the code.. But still nothing

Though at

require 'ferret'
module FerretConfig
  include Ferret

  INDEX = =>


I just changed  'f:/home/jan/workspace/ferret_test/index') to  'index'
since it is root in my folder and i'm on OSX tiger

Re: Dynamic Search tool help

Hello, I think I may have an answer to your problem.
First you need to install Ferret, you do this by typing:

gem install ferret

in your command prompt to download the ferret gem. Good.

Next you need to download the ActiveSearch plugin at: … rch+Plugin

The SVN repositry is at : … ple_search

And you can get this with subversion. Just go to the directory of your choice in command prompt, then type "svn checkout"
and it will download the plugin to the directory.

In your rails app, put the simple_search folder in your vendor/plugins directory.

Now lets assume you have a model called "Post" and you want to be able to search it, your model has to look something like this:

class Post < ActiveRecord::Base
  indexes_columns :into=> 'idx'

You can index any columns you like, indexes_columns :into => 'idx' will index all the columns
so you can search all of them. But you can also decide to index only a few, like:

indexes_columns :title, :body, :into => 'idx'

So the indexes_colums :into => 'idx', is for the simple search plugin, This means that in your posts table, there should be a column called idx which should be of the type "TEXT".

Now in your controller, assuming you want to save a post, you can do it like:

@post =[:post]) /* using the parameters from the form you've submitted */
@post.idx = @post.index_repr /* This sets the idx column to the search parameters */
#do whatever

This is how I implemented updates to post, so that when I update my post, the idx column also changes.

      # find our post
        @post = Post.find(params[:id])
        #Add the idx to the post as well
        new_idx = {"idx" =>[:post]).index_repr}
        if @post.update_attributes(params[:post])

Now if you want to do a search, your search method could be:

  def post_search
    @posts = Post.find_using_term(params[:q].gsub(/\\/, '\&\&').gsub('\'', "''"))

or simply, Post.find_using_term("dog")

Good luck.

Last edited by daibatzu (2006-07-08 11:03:33)

Re: Dynamic Search tool help

ok, I have been going over this for a week now.. I think I have it understood.. Just a few questions though.. less say I dont have the Class Post and instead have class Favorite should I replace @post = Post.find(params[:id]) to
@post = Favorite.find(params[:id]) and so on where ever a class or method appers ,, or is it set in stone ? like I am sure params[:post] is defined hash in the source somewhere

Sorry still learning.. Reading Ruby for rails right now..

Last edited by tripdragon (2006-07-18 07:36:10)

Re: Dynamic Search tool help

Hi Tripdragon, using @post = Post.find(params[:id]) is just a convention. You'll notice that @post is singular because I'm only finding one item (Post.find(3) i.e post with id of 3 for instance). If I wanted to find a large number of posts, I could say @posts = Post.find(:all). It's really just a convention.
I could say @dog = Post.find(params[:id]) or @dragons = Post.find(params[:id]), it's just a variable. It's just that the naming convention makes it easier for others and yourself to understand ruby/rails code.

Re: Dynamic Search tool help

Ah! cool
so is there somewhere that lists all of these params ? I see them a lot and think it would be good to know of my options for them..

Re: Dynamic Search tool help

The "params" hash is just data retrieved from the URL or POST parameters. Here are a list of URLs and resulting params to give you an idea:
params[:controller] => "products"
params[:action] => "show"
params[:id] => "3"
params[:controller] => "products"
params[:action] => "index" … y_id=>5
params[:controller] => "products"
params[:action] => "search"
params[:category_id] => "5"

This is for the usual :controller/:action/:id default setup which can be customized in the config/routes.rb file.

Railscasts - Free Ruby on Rails Screencasts

Re: Dynamic Search tool help

sigh,,, I can't get it

I did how ever get this to start working but it's not the best solution I know

I badly need a search engine that can have filters like a check box text-field that passes booleans or search in here :code

I dont want to ask to much of you ... But is there anyway you could share a working example package rails folder that uses this search tool ? If not I understand it looks like I am digging, when I really just want to learn..


Last edited by tripdragon (2006-07-19 01:33:25)

Re: Dynamic Search tool help

I don't have any experience with it, but have you seen the acts_as_farret plugin? It looks like what you might be after.

Just a suggestion, if this is your first Rails project, you may want to consider doing a simpler project first (such as the tutorial in the Agile Web Development with Rails book). I know doing small projects always helps me tackle the bigger/more complicated stuff.

Railscasts - Free Ruby on Rails Screencasts

Re: Dynamic Search tool help

Actually I did that book.. It has a nact of not explainging to many things that I had to find out in ruby for rails. But even so I am still lost.. Code is not always the easiest thing for me. but I am seeing that rails does not do search very well from what I read.. Which sucks, cause  I am trying to replace filemaker over here with this rails but it's not been the best of times as I thought it would be...