Tuesday, September 30, 2008

Ferret on shared hosting

I'm using ferret for a full text search on an app deployed on site5. These are the steps I took to get it set up:

1. Install the ferret gem

2. Tell the rails app where to find the gem:
# config/environment.rb
# Be sure to restart your server when you modify this file

# Uncomment below to force Rails into production mode when
# you don't control web/app server and can't set it the proper way
ENV['RAILS_ENV'] ||= 'production'

if ENV['RAILS_ENV'] == 'production'
ENV['GEM_PATH'] = '/home/forfolks/gems:/usr/lib/ruby/gems/1.8'
end


It seems that it is necessary to do this in environment.rb and not in config/environments/production.rb.

3. On shared hosting you can't run daemons so the DRb server approach for updating the ferret index won't work. I followed instructions here to set up a rake tast to update the index instead. I also commented out the production part in config/ferret_server.yml.

  # app/models/product.rb
def before_save
# disable automatic ferret indexing...move it to a cron job
self.disable_ferret(:always)
end


# ferret_index.rake
desc "Updates the ferret index for the application."
task :ferret_index => [ :environment ] do | t |
Product.rebuild_index
# here I could add other model index rebuilds
puts "Completed Ferret Index Rebuild"
end


I then call this in my code via:
    system "rake ferret_index &"

as detailed in this railscast.

No comments: