Solr Search with one to many associations

16 views
Skip to first unread message

Kishore Seemala

unread,
Feb 5, 2014, 4:04:03 AM2/5/14
to rubyonra...@googlegroups.com
I have a 2 entities which have many to many relation say teacher and
slot. Each teacher has many classes he/she takes up and each class is a
slot. I want to perform search on teacher by sunspot solr for all the
teacher who takes classes for different days in specific time.


Say model structures and relations as follows

class Teacher < ActiveRecord::Base

attr_accessible - name, address, phone
has many :slots

searchable do

string :day, multiple => true, stored => true do
slots.map(&:day)
end

string :start_time, multiple => true, stored => true do
slots.map(&:start_time)
end

string :start_time, multiple => true, stored => true do
slots.map(&:end_time)
end
end
end

class Slot < ActiveRecord::Base
attr_accessible - day, start_time, end_time teacher_id
belongs_to :teacher
end

The possible values for day attribute is slot are "Sunday", "Monday",
"Tuesday", etc
and start and end time would be "2:00pm", "6:00pm", etc in the database

How would I perform solr search query for the teachers who are having
for 3 days Monday, Tuesday and Wednesday between the time "2:00pm" and
"5:00pm"

I am trying with following code which is not taking "AND" condition to
retrieve only the teachers having classes all 3 days rather taking as
"OR" condition.

Sunspot.search(Teacher) do
with(:day, "Monday")
with(:day, "Tuesday")
with(:day, "Wednesday")
with(:start_time).greater_than_or_equal_to("2:00pm")
with(:end_time).less_than_or_equal_to("5:00pm")
end.results

I am using solr version 3.5 and sunspot_rails (2.0.0) and sunspot_solr
(2.0.0) gems for solr in rails and my rails version is 3.2.6

--
Posted via http://www.ruby-forum.com/.
Reply all
Reply to author
Forward
0 new messages