What I'm trying to accomplish (if it's possible) is to create a query that will essentially do 2 searches and return the results into one instance variable. The reason for this is because, I am using the will_paginate
gem and it only allows me to render an array of objects from the single query and doesn't accept anything else to be pushed onto that array being sent to the template.
i.e.
My events_controller.rb
#returns all events that match query
@events_search_result = Events.search(params[:event][:query])
#returns all events where column 'repeats' is nil
@events_not_repeat = Events.where.not(repeats: nil)
@events_not_repeat.each do |event|
#'schedule.occurs_on?' methods coming from different gem. Not relative in the outcome of the array of objects to be rendered.
if event.schedule.occurs_on?(params)
@events_search_result << event
end
end
Pushing these two array of objects gives me the desired result, but when trying to render @events_search_result
to the template, I get the error...
NoMethodError - undefined method 'total_pages' for #<Array:0x00000000>:
Which is coming from the will_paginate
gem.
Ideally, I'd like the two queries to happen in one shot, which is what I believe will solve my issue. I've looked at the ActiveRecord Query Interface
but due to my inexperience, I'm not that familiar with the query combinations that can be achieved.
Any advice will help a lot. Been stuck on this issue for quite a while now and tried a bunch of workarounds. Apologize in advance if my examples aren't descriptive enough but I'd be happy to fill in the details if need be. Thanks!
On the instance variable being sent to the template call ...
@combined_events_of_both_queries.paginate(:page => params[:page], :per_page => 5)
to change it to awill_paginate
array to be iterated over.