views:

429

answers:

2

I have a products model set up that I am trying to search with Thinking Sphinx. The model has an attribute called status which can be Active, Not active or Active during specified dates.

I would like to be able to restrict my search results to products that are active. I.e. has status of active or has status of active during dates and the current time is between those dates.

I'm a beginner to Rails so I'm looking for suggestions as to how I could implement this. I thought about putting a boolean method in my model that calculates this logic, but I don't think that this is indexable by Sphinx.

I am using MySQL as the database server.

Does anyone have any bright ideas?

+3  A: 

You're right, ruby methods on your model are not accesible to sphinx. However you can re-create the method as a sphinx attribute. These can easily be made using SQL fragments like so:

class Person < ActiveRecord::base
  ...


  define_index do
    has "status = 'active' and now() > start_date and now() < end_date", :as => :active, :type => :boolean
  end

  ...
end

Using a string to specify a field or attribute like this is the same as specifying a custom column when building an SQL query.

James Healy
Thanks so much! That looks like it'll work a treat.
Caps