Ferrets Gone Wild

Sometimes, acts_as_ferret can get a bit squirrely. I’m talking about a helper in a Ruby on Rails application in case you’re lost. The problem I had was that my index was jacked up. So what do we need to do about it? Rebuild the index! It’s the only option that I know of right now.

Two options that I’m clued into:

1. Nuke indexes out of the index directory
2. Call the class method rebuild_index

So what I did first was option number one. I didn’t really like that, although it is definitely easy. But I don’t want everyone doing that option when the application is being maintained down the road. The I’m not sure how root access or file privileges are involved either, but I’m betting that number two is going to work for you if you’re an application maintainer.

I want a rake task for option two. We know that rake can use ruby, so let’s do it. In your application’s lib/tasks directory, create a .rake file. Let’s call it utils.rake. It’ll be useful later on if you have other utility-type tasks that you might want.

Inside this file put:

namespace :utils do
  namespace :ferret do
    desc "Rebuild the ferret indexes for Kodo and Podo"
    task :rebuild => :environment do
      [Kodo, Podo].each do |klass|
        puts "rebuilding the Ferret /#{RAILS_ENV}/ index for : #{klass}.."
        klass.rebuild_index
        puts "  done."
      end
    end
  end
end

Dar would be proud. The only real improvements might be to detect which models utilize acts_as_ferret and some error handling.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: