搜索ajax_datatable_rails中的列?

时间:2014-12-16 09:09:25

标签: ruby-on-rails ruby

class Song < ActiveRecord::Base
  belongs_to :artist
  belongs_to :album
end  
class Album < ActiveRecord::Base
  has_many :songs
end
class Artist < ActiveRecord::Base
  has_many :songs
end

我在datatable类songs_datatable.rb中的查询是

class SongDatatable < AjaxDatatablesRails::Base
  def get_raw_records
    Song.joins(:artist, :album).select("songs.id, songs.artist_id, songs.album_id, songs.name as song_name, songs.location as song_location, artists.name as artist_name, albums.name as album_name")
  end
end  

songs.name,artists.name,songs.location,albums.name的可搜索列的名称应该是什么。

1 个答案:

答案 0 :(得分:0)

def searchable_columns
  @searchable_columns ||= [
    'songs.name',
    'artists.name',
    'songs.location',
    'albums.name'
  ]
end

注意:只能使用数据库列作为排序或搜索字段,因此artist_name之类的别名将无效。