Skip to content

Sorting on a model method (not a database column) #139

Open
@twicebishop

Description

@twicebishop

Is there any way I can configure AjaxDatatablesRails to sort based on a model method? It's working great on all the other columns based on database columns, but I get errors when attempting a model method.

In the example below, I'd like to search based on the model method "row_total":

class SearchTimecardRowDatatable < AjaxDatatablesRails::Base
  def sortable_columns
    @sortable_columns ||= ['Timecard.start_date',
                           'TimecardRow.monday',
                           'TimecardRow.tuesday',
                           'TimecardRow.wednesday',
                           'TimecardRow.thursday',
                           'TimecardRow.friday',
                           'TimecardRow.saturday',
                           'TimecardRow.sunday',
                           'TimecardRow.row_total'
                           'Timecard.timecard_state']
  end
....
end

class TimecardRow < ActiveRecord::Base
  def row_total
    0.0 +
    (self.monday    || 0.0) +
    (self.tuesday   || 0.0) +
    (self.wednesday || 0.0) +
    (self.thursday  || 0.0) +
    (self.friday    || 0.0) +
    (self.saturday  || 0.0) +
    (self.sunday    || 0.0)
  end
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions