Rails 5: Exporting Ransack result set with axlsx

I’m trying generate Excel file using axlsx gem based on Ransack gem result set.

controller:

@q = Candy.ransack(params[:q])
@candies = @q.result.all

When I call @candies with parameters like “chocolate” in the view using Ransack gem,
I get 30 or so results out of 600. It was successfully filtered!

But when I download @candies using axlsx using:

//index.xlsx.axlsx

    require 'axlsx'
    xlsx_package = Axlsx::Package.new

    workbook = xlsx_package.workbook

    workbook.add_worksheet(name: "Candies") do |sheet|
    sheet.add_row ["id", "name", "type", "date"]

        @candies.each do |candy|
             sheet.add_row [candy.id, candy.name, candy.type, candy.date]
        end
    end

It generates file with all 600 records!

This question very similar to Ransack Search Results – to_xls?
However I’ve encountered same problem using axlsx gem instead of to_xls gem!

Rails 5: Exporting Ransack result set with axlsx