views:

46

answers:

1

Using the tutorial "Find Stuff (quick and Dirty)" in "Advance Rails Recipes", I can find stuff in the development environment but not in the production environment.

NoMethodError: undefined method `searchable_by'

I have this code in "lib/searchable.rb"

module Searchable

  def searchable_by(*_column_names)
    @search_columns = []
    [column_names].flatten.each do |name|
      @search_columns << name
    end
  end

  def search(query, fields=nil, options={})
    with_scope :find => {
      :conditions => search_conditions(query, fields) } do
      find :all, options
      end
  end

  def search_conditions(query, fields=nil)
    return nil if query.blank?
    fields ||= @search_columns

    words = query.split(",").map(&:split).flatten

    binds = {}
    or_frags = []
    count = 1

    words.each do |word|
      like_frags = [fields].flatten.map { |f| "LOWER(#{f}) LIKE :word#{count}" }
      or_frags << "(#{like_frags.join(" OR ")})"
      binds["word#{count}".to_sym] = "%#{word.to_s.downcase}%"
      count += 1
    end

    [or_frags.join(" AND "), binds]
  end
end

and this in my 'config/initializers/core_extensions.rb'

ActiveRecord::Base.extend Searchable

I have roamed the internet and found people saying that if I change config.cache_classes to false then it should work: but it doesn't.

I've never tried using extra scripts in my apps, so I'm probably missing out something pretty basic here.

Any help will be appreciated!

UPDATE: Additional Info

I specify this in the model (app/models/candidate.rb)

class Candidate < ActiveRecord::Base
  searchable_by :first_name, :surname, :experience, :skills, :looking_for, :hours_required, :location, :more_details
end

And I call it in the controller (app/controllers/candidates_controller.rb)

class CandidatesController < ApplicationController
  def index
    @candidates = Candidate.visible
  end

  def search
    @candidates = Candidate.visible.search(params[:q])
  end

end

(visible is just a named_scope)

Any help would be really appreciated.

+1  A: 

you have a typo on searchable_by *_column_names should be *column_names, once that is fixed, be sure to restart your rails server.

Dan
thanks for your eagle eyes!
Joe