Custom Filter Method
When the built-in filtering doesn't fit (for example, filtering on an associated table), pass filter_with: a model scope that applies the filter.
Quick exampleβ
scope :by_company_name, lambda { |value, condition = nil|
left_joins(:company).ct_string_filter_condition(column: :name, value:, condition:, table: "companies")
}
filter :company_name, :string, filter_with: :by_company_name
Optionsβ
Syntaxβ
filter :filter_name, :filter_type, filter_with: :custom_filter_method
Scope signatureβ
| Filter type | Scope arguments |
|---|---|
:search | (value) |
:single_select, :multi_select | (value, condition = nil) |
:date, :range | (value, condition = nil) |
:string | (value, condition = nil) |
condition is the operator selected in Advanced Filters, for example is_not or greater_than. It is nil when the scope is called from the standard filter bar. For date and range filters, value is the raw string ("2026-01-01 to 2026-01-31", "18 to 30", or a single value such as "18").
Lambda scopes enforce arity. A select, date, range, or string scope that does not accept condition raises ArgumentError.
Recipesβ
Searchβ
class YourModel < ApplicationRecord
scope :custom_filter_method, ->(value) { where('column_name ILIKE ?', "%#{value}%") }
end
filter :filter_name, :search, filter_with: :custom_filter_method
Single / Multi Selectβ
class User < ApplicationRecord
scope :by_company_country, lambda { |value, condition = nil|
left_joins(:company).ct_dropdown_filter_condition(column: :country, value:, condition:, table: 'companies')
}
end
filter :company_country, :multi_select, helper_method: :company_country_options, filter_with: :by_company_country
Stringβ
class User < ApplicationRecord
scope :by_company_name, lambda { |value, condition = nil|
left_joins(:company).ct_string_filter_condition(column: :name, value:, condition:, table: 'companies')
}
end
filter :company_name, :string, filter_with: :by_company_name
Date and Rangeβ
class User < ApplicationRecord
scope :by_company_size, lambda { |value, condition = nil|
left_joins(:company).ct_range_filter_condition(column: :employee_count, value:, condition:, table: 'companies')
}
end
filter :company_size, :range, filter_with: :by_company_size
Condition helpersβ
These helpers are available on every ActiveRecord model through CmAdmin::FilterConditions. table: defaults to the model's own table.
| Helper | Handles |
|---|---|
ct_dropdown_filter_condition(column:, value:, condition:, table:) | is, is_not, is_empty, is_not_empty. cm_empty maps to NULL |
ct_range_filter_condition(column:, value:, condition:, table:) | between, not_between, greater_than, less_than, greater_than_or_equal_to, less_than_or_equal_to, is, is_not, is_empty, is_not_empty |
ct_string_filter_condition(column:, value:, condition:, table:) | is, is_not, contains, does_not_contain, is_empty, is_not_empty. Case-insensitive, literal matches (wildcards escaped); is_empty also matches '' |
If you write the logic by hand instead, handle is_empty / is_not_empty. They are called with a blank value.