There are two approaches to custom filters with joined tables. The first thing that comes to mind is to define
a column of a joined table with
:attribute
and
:assoc
, and submit a list of all possible values of this column to
:custom_filter
, like it is done in the example below in the Priority column:
g.column name: 'Priority', attribute: 'name', assoc: :priority, custom_filter: %w(Anecdotic High Low Normal Urgent) do |task|
This works but such a filter produces a query with a
WHERE
clause comparing a varchar field with one of the string values submitted to
:custom_filter
.
This is not guaranteed to be as efficient as comparing an indexed integer foreign key with an integer primary key, thus, this approach is highly advised against.
To implement filtering by foreign keys, define the column with the foreign key in
:attribute
and submit a hash or a two element array containing the IDs and labels of the joined table to
:attribute
.
This has a negative side effect on sorting - the column will now be sorted according to the numerical value of the foreign key.
It can be dealt with by overiding sorting by
:custom_order
in
initialize_grid
:
custom_order: {'tasks.status_id' => 'statuses.position', 'tasks.project_id' => 'projects.name'}
# encoding: utf-8 class CustomFilters2Controller < ApplicationController def index @tasks_grid = initialize_grid(Task, include: [:priority, :status, :project], order: 'statuses.name', custom_order: { # 'tasks.priority_id' => 'priorities.name', 'tasks.status_id' => 'statuses.position', 'tasks.project_id' => 'projects.name' } ) end end
.well %h2= current_page_title %p There are two approaches to custom filters with joined tables. The first thing that comes to mind is to define a column of a joined table with %code :attribute and %code :assoc , and submit a list of all possible values of this column to %code :custom_filter , like it is done in the example below in the Priority column: %p %code g.column name: 'Priority', attribute: 'name', assoc: :priority, custom_filter: %w(Anecdotic High Low Normal Urgent) do |task| %p This works but such a filter produces a query with a %code WHERE clause comparing a varchar field with one of the string values submitted to %code :custom_filter \. This is not guaranteed to be as efficient as comparing an indexed integer foreign key with an integer primary key, thus, this approach is highly advised against. %p To implement filtering by foreign keys, define the column with the foreign key in %code :attribute and submit a hash or a two element array containing the IDs and labels of the joined table to %code :attribute \. This has a negative side effect on sorting - the column will now be sorted according to the numerical value of the foreign key. It can be dealt with by overiding sorting by %code :custom_order in %code initialize_grid \: %p %code custom_order: {'tasks.status_id' => 'statuses.position', 'tasks.project_id' => 'projects.name'} = show_code .row-fluid .col-md-12 = render 'grid'
<%= grid(@tasks_grid) do |g|
g.column name: 'ID', attribute: 'id', filter: false
g.column name: 'Title', attribute: 'title'
g.column name: 'Priority', attribute: 'name', assoc: :priority, custom_filter: %w(Anecdotic High Low Normal Urgent) do |task|
task.priority.name if task.priority
end
g.column name: 'Status', attribute: 'status_id', custom_filter: Status.to_dropdown do |task|
task.status.name if task.status
end
g.column name: 'Project Name', attribute: 'project_id', custom_filter: Project.to_dropdown do |task|
task.project.name if task.project
end
g.column name: 'Archived', attribute: 'archived' do |task|
task.archived? ? 'Yes' : 'No'
end
g.column name: 'Added', attribute: 'created_at' do |task|
task.created_at.to_s(:short)
end
g.column do |task|
link_to('Edit', edit_task_path(task))
end
end -%>
ID | Title | Priority | Status | Project Name | Archived | Added | |
---|---|---|---|---|---|---|---|
21-40 / 500 show all | |||||||
127 | doloribus ut | Assigned | Divine Firmware | No | 06 Jun 12:37 | Edit | |
232 | eos quam dolorem | Low | Assigned | Ultimate Website | No | 18 Jun 12:37 | Edit |
3 | aliquam repudiandae | Anecdotic | Assigned | Divine Firmware | No | 21 Mar 12:37 | Edit |
44 | id temporibus eligendi | Assigned | Ultimate Website | No | 27 Mar 12:37 | Edit | |
26 | quia mollitia deleniti | Urgent | Assigned | Super Game | No | 23 May 12:37 | Edit |
61 | quia architecto | Normal | Assigned | Divine Firmware | No | 15 May 12:37 | Edit |
142 | maxime accusamus consectetur | Assigned | Super Game | No | 31 May 12:37 | Edit | |
145 | asperiores | Low | Assigned | Super Game | No | 05 Jun 12:37 | Edit |
270 | modi minus | Normal | Assigned | Super Game | No | 04 May 12:37 | Edit |
273 | vero | High | Assigned | Super Game | Yes | 11 May 12:37 | Edit |
16 | omnis | High | Assigned | Super Game | No | 21 Mar 12:37 | Edit |
91 | quaerat | High | Assigned | Divine Firmware | No | 08 Jun 12:37 | Edit |
2 | ut sint | Anecdotic | Assigned | Divine Firmware | No | 09 Apr 12:37 | Edit |
158 | aut voluptatibus aut | Normal | Assigned | Super Game | No | 12 Apr 12:37 | Edit |
53 | molestiae | Assigned | Super Game | No | 21 Apr 12:37 | Edit | |
62 | eum velit saepe | Normal | Assigned | Divine Firmware | No | 15 Mar 12:37 | Edit |
46 | facere | Anecdotic | Assigned | Ultimate Website | Yes | 26 Apr 12:37 | Edit |
80 | corrupti officia | Urgent | Assigned | Super Game | No | 12 Mar 12:37 | Edit |
312 | repellendus perferendis vitae | Low | Assigned | Super Game | No | 17 May 12:37 | Edit |
302 | eligendi vel | Normal | Assigned | Super Game | No | 24 Mar 12:37 | Edit |