Rails tips: ActiveAdmin access filter collection
Since ActiveAdmin uses Ransack for search, we can use Ransack to grab filter collection using their params. This is old method but it works fine to my Rails 5.0.0 and ActiveAdmin 2.0.0 version.
Here is an example
controller do def index if params[:q].present? && params[:q][:work_types_in].present? index! do |format| work_types = params[:q][:work_types_in] params[:q].delete('work_types_in') @users = User.ransack(params[:q]).result @users = @users.including_any_work_types(work_types).page(params[:page]).per(25) format.html end else super end end end
In this example I’m trying to implement different filter logic for work types filter query. If work types value is present, we have to implement separate filter logic to the results from ransack. If not present we can call super so that it does not effect to the work types filter.
Filter collection without work types query value is:
@users = User.ransack(params[:q]).result
Here I have deleted the work_types params from the query so that it will not effect to the ransack result.