Wednesday, March 14, 2007

Pitfalls of acts_as_ferret, with DrbServer to the rescue

I don't know how general this case is, but I've found that if you want to use acts_as_ferret in your production Rails app, you need to run your Ferret server as a separate DrbServer process.

Background

acts_as_ferret gives you a powerful search capability, and it's much easier to implement than MySQL full text querying.

Numerous tutorials (here and here) describe how to do this. In development mode, it's as simple as adding something like this to your model:

acts_as_ferret :fields => [ :city, :state ]

and this to your controller:

@users = User.find_by_contents(params[:keywords])

Problem

I started getting all kinds of corrupted index errors when I put this into production, because acts_as_ferret can't handle multiple separate processes access the index at once. This caused a lot of scrambling as I was starting to get a lot of traffic.

Solution

Fortunately I discovered that acts_as_ferret comes with its own built-in DrbServer. As soon as I set that up, everything worked great.

The developer does a great job explaining what to do. You add :remote => true to your acts_as_ferret declaration, setup a ferret_server.yaml config file, then run:

RAILS_ENV=production script/runner vendor/plugins/acts_as_ferret/script/ferret_server

...and you're golden.

UPDATE: To make the acts_as_ferret unit tests run correctly, add the following line to your test/environment.rb file:

AAF_REMOTE = true

9 comments:

Anonymous said...

Hi,just a quick note: the AAF_REMOTE variable isn't needed unless you want to run the unit tests of acts_as_ferret with the DRb server.

Mike Subelsky said...

Thanks -- I updated the post to make that more clear!

Johannes Fahrenkrug said...

If you don't want to use the drb server for your tests, but want them to run correctly anyway, see this blog post: Unit testing with acts_as_ferret

Ravi Vardhan said...

Hi,

I got the same problem with ferret.
but i cann't understand online in your solution.

RAILS_ENV=production script/runner vendor/plugins/acts_as_ferret/script/ferret_server

how to run this server?

before my rails server(apache) or when and where this command i use?

please give me explanation for this.

thanks,

Anonymous said...

I try to run:

script/ferret_server -e production start

on my production system and it says:

can't convert nil into String

Any ideas how to track this down? It works fine on my development system (MacOS X), and I have copied the complete project over to the production system (FreeBSD), and installed ferret using the gem.

Any help would be greatly appreciated.

Leonardo Sanvitale said...

Does anyone knows how to solve Gideon problem (error: can't convert nil into string?).

Anonymous said...

I am having the same problem. I try to run "ruby script/ferret_server start" as outlined in the Advanced Rails Recipe book, and I get the message
"can't convert nil into String".

I am using instantrails, and windows vista.

Mike Subelsky said...

sorry, not sure what would cause this not to work on Windows

Anonymous said...

The three Munchkins bowed low to her and wished her a pleasant
journey, after which they walked away through the trees.

shoe carnival
nike sneakers
Nike Basketball shoes
MBT shoes
supra shoes