jbox-web / ajax-datatables-rails

A wrapper around DataTable's ajax methods that allow synchronization with server-side pagination in a Rails app
MIT License
584 stars 228 forks source link

Unable to order by computed field defined as method in model, only fields as in database columns are working. #392

Open sameer1612 opened 3 years ago

sameer1612 commented 3 years ago

Taking same city example:

How to proceed if I want to sort by "full_name"?

City model :

` class City < ApplicationRecord belongs_to :country

def full_name name + ' city' end end `

city_datatable.rb :

` def view_columns @view_columns ||= { check_box: { source: 'City.id', orderable: false, searchable: false }, name: { source: 'City.name' }, iata: { source: 'City.iata' }, country_name: { source: 'City.country_id', cond: filter_country_condition }, full_name: { source: 'City.full_name', searchable: false } } end

def data records.map do |record| { check_box: record.decorate.to_checkbox(selected: selected.include?(record.id)), name: record.name, iata: record.iata, country_name: record.country.try(:name), full_name: record.full_name } end end `

Haml file changes.

`


ERROR :

` Completed 500 Internal Server Error in 9ms (ActiveRecord: 1.0ms | Allocations: 2457)

ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: cities.full_name):

app/datatables/city_datatable.rb:15:in map' app/datatables/city_datatable.rb:15:indata' app/controllers/cities_controller.rb:15:in block (2 levels) in datatable' app/controllers/cities_controller.rb:14:indatatable' `