Dealing with Recursive Models
User wants to search for classes nearby according to date similar to classpass
Form fields for creating class by admin::
2) Start Date ( Class start date)
3) End Date
class Fpclass < ActiveRecord::Base after_create :build_ice_cube_params has_many :recursivedates private # This method is used to push dates between start date to end date (weekly) to recursivedates def build_ice_cube_params search_dates.each do |date| recursivedates << Recursivedate.create(:ondate => date) end end end
Implemented search using Ransack
class FiltersController < ApplicationController def search @c = Fpclass.ransack(params[:q]) # params[:q][:recursivedates_ondate_eq] @fpclasses = @c.result(distinct: true) end end
Example how it'll work
1) Admin creates class that'll held on every sunday until 2 months using (start date and end date )
2) Fpclass saves all data (name, startdate, enddate etc.,) and creates recursivedates (Between dates like 1st of july, 8th july, 15th july ....)
3) Getting fpclasses at search page using ransack on (recursivedates model)
Problems i've facing
1) When admin edits form then all fresh dates dumped into recursivedates model along with old ones
2) When admin gives longer dates(from 2015 to 2020) (Sql insertions are more)
3) Searching kills time
4) Hard to implement conditions at Reserve button as shown in ref image
Can there be any alternative approach or can you let me know if any mistakes i've done ?
Recurring events get complicated quickly! :)
You may need to do a find_or_create for all the recursivedates when you do an edit. It also might be easier to delete all the existing ones before adding the new dates. That will save you some trouble. I'd take a look at how Google Calendar does it and work backwards from there if you like the way it works.
I think they may calculate on the fly the recurring ones so they don't have to insert records for X years into the future and your calendar will always work. They probably separate individual dates and recurring ones and query for both each time the page renders.