Sorry the title isn't very descriptive, can't find a good way to sum up my problem.
Each location has an associated hours of operation record that I need to search on. The setup is as such:
...
class Hour < ActiveRecord::Base
attr_accessible :bar_id, :day, :start_time, :end_time
belongs_to :location
validates :location_id, :presence => true
validates :day, :presence => true
# Thinking Sphinx #############################################
define_index do
join locations
indexes
locations .name, :as => :location_name
indexes
locations .description, :as => :location_description
has start_time, :as => :open_time
has end_time, :as => :close_time
has :location_id
has :day
has "locations.latitude", :as => :lat, :type => :float
has "locations.longitude", :as => :lng, :type => :float
end
sphinx_scope(:is_open) { |date,start_time=14,end_time=61|
day_num = Date.strptime(date, "%Y-%m-%d").wday
with_time_range = "*, IF(open_time < #{end_time} AND close_time >= #{start_time}, 1, 0) AS in_range"
{ :sphinx_select => with_time_range, :with => { :day => day_num, 'in_range' => 1 }, :group_by => 'bar_id', :group_function => :attr, :group_clause => "@geodist ASC" }
}
###############################################################
end
This setup works pretty good when all the hours are the same, but if even one of the hours are outside of the range I am searching, that location isn't returned. Example:
If I search for 4pm - 7pm, it returns that location, but if I search for 5pm - 7pm, it misses that location.
I had previously searched through location model's association to hours, but that didn't give me any of the correct results, hence I arrive at this. So close,yet so far away.
Any help would be appreciated.