Bugs: Browse | Submit New | Admin

[#18483] Sphincter can add the "same" class multiple times to its indexes, when running in development mode.

Date:
2008-02-29 15:04
Priority:
3
Submitted By:
Andy Stewart (airblade)
Assigned To:
Eric Hodel (drbrain)
Category:
Sphincter
State:
Open
Summary:
Sphincter can add the "same" class multiple times to its indexes, when running in development mode.

Detailed description
Sphincter 1.1.0

With a Rails app running in development mode, classes by default are not cached.

Sphincter::Search.indexes is a hash of classes-to-be-indexed mapped to their indexing options.

The Sphincter::Search.add_index method, which is mixed into ActiveRecord::Base, adds any model class with an add_index
declaration to the Sphincter::Search.indexes hash.

The problem arises when a model class with an add_index is loaded more than once by Rails (in development mode).  Rails
does not cache model class definitions, so each new version of, say, the User class has a different object_id.  And
that causes the add_index method to add each new version to the Sphincter::Search.indexes hash.

This then breaks the mapping from sphinx id to database id, because the index_count is too high.

Here is a solution that works for me(TM):

In the file Sphincter-1.1.0/lib/sphincter/search.rb, the final line of the method add_index(options = {}) is:

  Sphincter::Search.indexes[self] << options

I make this conditional on a class with the same name as self not being in the hash:

  unless Sphincter::Search.indexes.keys.any? { |klass| klass.class_name == self.class_name }
    Sphincter::Search.indexes[self] << options
  end

This doesn't break any tests.

Add A Comment: Notepad

Please login


Followup

No Followups Have Been Posted

Attached Files:

Name Description Download
No Files Currently Attached

Changes:

No Changes Have Been Made to This Item