Question

Here's my problem, I'll try to be concise.

I have a model Course that has_many Planning. A Planning has a start_date and an end_date.

I want to retrieve (with sunspot) all the Courses that have a particular planning between a given start_date and an end_date.

The problem is that if I store in the model Course :

searchable do
  date :start_date, multiple: true do
    plannings.map(&:start_date)
  end

  date :end_date, multiple: true do
    plannings.map(&:end_date)
  end
  ...
end

I loose the fact that start_date and end_date are couples.

Is there a way / trick to store ranges?

Was it helpful?

Solution

Have almost the same problem, in my case start_date and end_date wasn't really a date, just a year (integer), solve it with multiple value fields as range:

searchable do
  date :course_date, multiple: true do
    plannings.map{|p| (p.start_date..p.end_date).to_a}
  end
  ...
end

so course_date contain all years when courses going. If you have 2001-2003 and 2005-2007(2001, 2002, 2003, 2005, 2006, 2007) and search with equal_to.

OTHER TIPS

A possibility would be to make your query in two times. First find the plannings, then find the courses restricting courses by ID.

Maybe not a very optimised one, but why try to optimize to soon...

Or to index hours with a function:

def happens_between_8_and_9
  return true if start_date > 8..
end

searchable do
  boolean :happens_between_8_and_9
  boolean :happens_between_9_and_10
end

```

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top