Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Steve Polito asked in Rails

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
...
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.