Skip to main content

How Do I Search Against Records Saved In Memory, and Not the Database?

Rails • Asked by Steve Polito

Problem

I have an event that starts Jan 1, 2017 and repeats every day until Dec 31, 2017. If I search for an event (using ransack) that starts after Jan 1, 2017 no events will appear, because the only record saved to the database has a start date of Jan 1, 2017. The rest of the records are created in memory. However, if I search for an event that starts before Jan 1, 2017, all results render as expected.

Setup

I have a list of repeating events. The repeating events are created using ice_cube and recurring_select, along with the following method in my model.

event.rb

    ...
def events_list(start)
    if recurring.empty?
      [self]
    else
    end_date = start.end_of_year
      schedule(start_time).occurrences(end_date).map do |date|
        Event.new(id: id, title: title, start_time: date, club: club)
      end
    end
  end
    ...

events_controller.rb

...
def all
      @q = Event.ransack(params[:q])
      @events = @q.result(distinct: true).order(start_time: :desc)
      @events_list = @events.flat_map{ |e| e.events_list(params.fetch(:start_date, Time.zone.now).to_date) }
      @events_list_paginated = @events_list.paginate(page: params[:page])
end
...

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.