0

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?

dlamblin
  • 43,965
  • 20
  • 101
  • 140
Caps
  • 1,493
  • 1
  • 16
  • 24
  • I was just thinking that one way of solving this would be to create an extra field called is_active and have a cron job run each night and set the field to true if the status is active or the current time is between the specified dates and set it to false otherwise. I think I would also need to override the save command in the model to do the same to individual products if their status was changed in the update. – Caps Jul 18 '09 at 02:30

2 Answers2

4

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
  • 14,557
  • 4
  • 33
  • 43