ActiveAdmin Access Filters Collection
I am trying to create a collection_action where I am going to do something with the entire collection of filtered items. My problem is, from within collection_action, I don't have access to the filtered collection. When I access collection
, these are only the elements that are on the first page of the posts. In my action_item, I have access to collection_before_scope
which is exactly the filtered entry I want, but it is empty when I try to access it from mine collection_action
.
Below is mine current setup
trying to find the collection I want.
collection_action :dosmth, :method => :get do
# collection_before_scope will be empty
puts "collection_before_scope = " + collection_before_scope.to_yaml
# collection will return only the filtered items on the front page
puts "collection = " + collection.to_yaml
redirect_to :back, notice: 'Something happening'
end
action_item :only => :index do
# collection_before_scope will return the full collection that I'm looking for.
puts "collection_before_scope = " + collection_before_scope.to_yaml
link_to "Export", dosmth_admin_earned_points_path(controller.params)
end
The closest related question I could find was this: ActiveAdmin Collection action on filtered data , which didn't seem to work for me.
Any help would be greatly appreciated.
Thank,
Update:
I still have the same problem, but I figured out something. If I try to access the collection before collection_before_scope
then the correct filtered items are in collection_before_scope. I don't want to be able to access the collection to get the correct one collection_before_scope
. Not sure why this will happen.
collection_action :dosmth, :method => :get d0
# collection will return only the filtered items on the front page
puts "collection = " + collection.to_yaml
# collection_before_scope will be correct because I accessed the collection first. why???
puts "collection_before_scope = " + collection_before_scope.to_yaml
redirect_to :back, notice: 'Something happening'
end
source to share
Try the following:
puts "filtered collection = " + apply_filtering(collection).to_yaml
(before the call collection
)
Why are you getting the correct filtered collection after the first call to the collection?
The method collection
will call the method find_collection
: https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_controller/data_access.rb#L32
The method find_collection
will call the method apply_filter
: https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_controller/data_access.rb#L50
And as soon as the method was called collection
:
https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_controller/data_access.rb#L22-L27
source to share