Search code examples
mysqlruby-on-railsactiverecordransack

Ransack Search Query with conditions group and sum


How can I search from 'start date' to 'end date' and sum a column after the search result with ransack and also group?

I can search from start to end date but I am stuck where I have to sum and group

eg. History.group(:name).sum(:produced)

I want to be able to call that after the search result from "Start date" to "End date"


Solution

  • The result method of Ransack::Search returns a relation so the solution is:

    scope = History.search({id_eq: 1}).result
    scope.group(:name).sum(:produced)