Search code examples
ruby-on-railswill-paginatebackgrid

Sort on 'first_name' instead of 'full-name' in Backgrid


Using backgrid, displaying users list. 'User' model having first_name and last_name attributes. I have wrote method full_name on user model and displaying that full name into backgrid.

{
  name: "full_name",
  label: "Name",
  editable: false,
  cell: 'string'
}

On server side using will_pagination. When I clicks on full_name to sort. It gives error - "ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column "full_name" does not exist".

So I need to sort on first_name instead of full_name. But I don't know how to do? Any suggestions?


Solution

  • On server side, this can be handled by overriding the param 'sort_by' value and order. We can pass multiple columns in order. In this case instead of 'full_name', applied order on first_name and last_name. For example:

    User.paginate(page: params[:current_page], per_page: params[:pageSize]).order("first_name asc, last_name asc")