Forums » Ferret » acts_as_ferret: dynamic index selection

acts_as_ferret: dynamic index selection
Posted by Stuart Rackham (Guest)
on 23.05.2006 04:50
Hi

What is the be practice regards setting the index path dynamically?

My application needs to switch indexes based on the logged in user, I
put the following before_filter into my controller (the
Document.index_dir method calculates the user's index path):

before_filter { Document.ferret_configuration[:path] = 
Document.index_dir }

It seems to work, the only rough edge is that the acts_as_ferret
declaration immediately creates a spurious index directory rather than
deferring until the index is actually accessed (and the index path
known), this also means the index directory is not automagically
created.

Cheers, Stuart
--
Stuart Rackham
Re: acts_as_ferret: dynamic index selection
Posted by Stuart Rackham (Guest)
on 25.05.2006 00:14
Stuart Rackham wrote:
> Hi
> 
> What is the be practice regards setting the index path dynamically?
> 
> My application needs to switch indexes based on the logged in user, I
> put the following before_filter into my controller (the
> Document.index_dir method calculates the user's index path):
> 
> before_filter { Document.ferret_configuration[:path] = 
> Document.index_dir }
> 
> It seems to work, the only rough edge is that the acts_as_ferret
> declaration immediately creates a spurious index directory rather than
> deferring until the index is actually accessed (and the index path
> known), this also means the index directory is not automagically
> created.

P.S. The above didn't quite work, needed to set index directory in two
places for it to function when adding to the index and searching:

In controller:

  before_filter { Document.set_index_dir }

In the model:

  # Set the Ferret index to that of the current users account.
  def self.set_index_dir
    # Two places are used internally by acts_as_ferret.
    Document.ferret_configuration[:path] = Document.index_dir
    Document.configuration[:index_dir] = Document.index_dir
  end

> 
> Cheers, Stuart
> --
> Stuart Rackham