1

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?

kd12
  • 1,291
  • 1
  • 13
  • 22

2 Answers2

1

You are sorting with full_name make it sortable with first_name. Don't use full_name in your will_paginate and backgrid configurations.

Sami
  • 1,041
  • 3
  • 16
  • 32
  • want to show full_name of user in table. Question is - How to set sorting on first_name instead of full_name in backgrid? – kd12 Aug 16 '13 at 08:30
  • in table if you want to add full-name in you table then you have to add it in attribute accessible in your model and generate and run a migration. – Sami Aug 16 '13 at 08:39
  • @users = User.paginate( page: params[:current_page], per_page: params[:pageSize] ).order("#{params[:sort_by]} #{params[:order]}") I want to show full_name into grid of users. – kd12 Aug 16 '13 at 09:08
  • In the question you are saying "I need to sort on first_name instead of full_name" now you are saying " I want to show full_name into grid of users". I cant understand what you wanna do. – Sami Aug 16 '13 at 09:32
  • Its means that I want show value of full_name into 'Name' column of users backgrid but at time of sorting, sort by first_name for this column 'Name'. Is it clear? – kd12 Aug 16 '13 at 09:39
  • Use ".sort_by" method – Sami Aug 16 '13 at 09:54
0

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")
kd12
  • 1,291
  • 1
  • 13
  • 22