Skip to main content

Select Options

:single_select and :multi_select form fields get their options in one of four ways: a static collection, the model's enum, a helper_method, or fetch_config β€” a model scope queried as the user types, for lists too large to render up front.

Quick example​

section "Mode Of Transport" do
form_field :status, input_type: :single_select, collection: [["Vehicle", "vehicle"], ["Bike", "bike"]]
form_field :transport_type_id, input_type: :single_select, helper_method: :transport_type_options
form_field :route_id, input_type: :single_select,
fetch_config: { model: "Route", scope_name: "catalyst_search" }
end
app/helpers/catalyst/custom_helper.rb
def transport_type_options(_record, _field_name)
Constant.where(type: "transport_type").map { |item| [item.full_name, item.id] }
end

Options​

The first match wins:

SourceOptionFormat
Remote scopefetch_config: {...}Options fetched remotely as the user types. See Remote options.
Helper methodhelper_method: :nameMethod in Catalyst::CustomHelper called with (record, field_name); returns [[label, value], ...].
Static listcollection: [...][[label, value], ...] or [value, ...].
EnumnoneUsed automatically when the field is an enum: [[key.titleize, key], ...].

Remote options​

fetch_config loads options remotely, keeping the list out of the page. The select queries Catalyst's fetch_options endpoint with what the user typed and loads results in pages of 10. The config travels as a signed payload, so only the declared model, scope and methods are reachable.

fetch_config works the same way on :single_select and :multi_select filters.

Just a model name​

model: is the only required key β€” scope_name defaults to "all", so this offers every User:

form_field :user_id, input_type: :single_select, fetch_config: { model: "User" }

Typing narrows the list through User's index :search filters (see How search is applied) β€” if the model has none, every option shows regardless of what is typed. The model must define ct_display_name for the option labels β€” or set title_method.

Config keys​

KeyTypeDefaultDescription
modelStringrequiredModel that provides the options.
scope_nameSymbol"all"Scope called on the model.
dependent_fieldsArray[]Other fields or filters whose current values are passed to the scope.
title_methodSymbol:ct_display_nameMethod called on each record for the option label.
value_methodSymbol:idMethod called on each record for the option value.
override_searchBooleanfalseSkip the model's index :search filters and let the scope own searching.

What the scope receives​

The scope is called as Model.<scope_name>(**args) with these keywords:

KeywordOn formsOn filtersValue
search:yesyesWhat the user typed β€” "" on first open.
<dependent name>:yesyesOne keyword per dependent_fields entry: the other control's current value. Always sent, nil when empty.
object_class:yesβ€”Class name of the record the form edits ("Student", or the nested record's class inside nested_form_field).
object_id:yesβ€”The record's id. Only sent when the record is persisted β€” give it a nil default so new forms work too.
  • A dependent value arrives as a scalar for one selection or an array for a :multi_select β€” use Array() to normalize.
  • Declare every keyword the config can send (->(search:, brand_id: nil, object_class: nil, object_id: nil)) or catch all of them (->(**args)). A scope that rejects any keyword is called with no arguments β€” it still works, but loses search: too. A scope taking one positional argument receives the whole hash.
  • object_class:/object_id: let a scope load the record being edited and tailor options to it β€” see Scope options to the record being edited.
app/models/user.rb
scope :catalyst_search, ->(search:, ct_role_id: nil, object_class: nil, object_id: nil) {
records = where("first_name ILIKE :q OR last_name ILIKE :q OR email ILIKE :q", q: "%#{search}%")
records = records.where(ct_role_id:) if ct_role_id.present?
records
}

Dependent fields​

dependent_fields wires one control to another: when the named control's value changes, this select clears its selection and re-queries the scope with the new value.

  • On a form_field it names sibling form_fields of the same object.
  • On a filter it names other filters' db_column_names β€” see Remote options.
  • Inside nested_form_field, prefix a name with parent. to read a field from the parent form. The scope receives it with dots flattened to underscores (parent.vendor_id β†’ parent_vendor_id:).

How search is applied​

Typed text reaches the records in two places:

  1. The scope gets it in search: β€” handle it there or ignore it.
  2. The results are then narrowed through the fetch model's index :search filters β€” the same columns its index search uses. A scope that ignores search: (-> { order(:name) }) still becomes searchable when the model has a :search filter.

Set override_search: true to skip step 2 β€” the scope alone decides what matches. See Own the search in the scope.

Labels for already-selected values are looked up with value_method when it names a database column (Model.where(value_method => ids)); when it doesn't, collection or helper_method supplies them.

Recipes​

Refill options when another field changes​

section "Vehicle" do
form_field :brand_id, input_type: :single_select, fetch_config: { model: "Brand" }
form_field :product_id, input_type: :single_select,
fetch_config: { model: "Product", scope_name: "catalyst_search",
dependent_fields: [:brand_id] }
end
app/models/product.rb
scope :catalyst_search, ->(search:, brand_id: nil) {
products = where("name ILIKE ?", "%#{search}%")
brand_id.present? ? products.where(brand_id:) : products.none
}

Changing the brand clears the product selection and reloads the list β€” products.none keeps it empty until a brand is picked.

Depend on a parent field inside nested_form_field​

Inside a nested form, dependent_fields resolves against the nested row β€” prefix with parent. to read a field of the record the form edits. Here each order line's products are limited to the order's chosen vendor:

section "Order" do
form_field :vendor_id, input_type: :single_select, fetch_config: { model: "Vendor" }
nested_form_field :line_items do
form_field :product_id, input_type: :single_select,
fetch_config: { model: "Product", scope_name: "catalyst_search",
dependent_fields: ["parent.vendor_id"] }
form_field :quantity
end
end
app/models/product.rb
scope :catalyst_search, ->(search:, parent_vendor_id: nil) {
products = where("name ILIKE ?", "%#{search}%")
parent_vendor_id.present? ? products.where(vendor_id: parent_vendor_id) : products
}

Own the search in the scope​

override_search: true skips the index :search filters so the scope alone decides what the typed text matches β€” for example when it searches an associated table:

form_field :assignee_id, input_type: :single_select,
fetch_config: { model: "User", scope_name: "catalyst_search_assignable", override_search: true }
app/models/user.rb
scope :catalyst_search_assignable, ->(search:) {
joins(:profile).where(active: true)
.where("profiles.display_name ILIKE :q OR users.email ILIKE :q", q: "%#{search}%")
}

Scope options to the record being edited​

On forms the scope also receives object_class: and object_id: β€” the class and id of the record the form edits (object_id is absent on new). Load the record to tailor options β€” here a shipment's hubs are limited to its own region:

form_field :hub_id, input_type: :single_select,
fetch_config: { model: "Hub", scope_name: "catalyst_search" }
app/models/hub.rb
scope :catalyst_search, ->(search:, object_class: nil, object_id: nil) {
hubs = where("name ILIKE ?", "%#{search}%")
if object_class == "Shipment" && object_id.present?
hubs = hubs.where(region_id: ::Shipment.find(object_id).region_id)
end
hubs
}

Custom labels and values​

title_method and value_method pick what shows in the option and what is submitted:

form_field :country_id, input_type: :single_select,
fetch_config: { model: "Country", scope_name: "catalyst_search",
title_method: :name_with_flag }
app/models/country.rb
def name_with_flag = "#{flag_emoji} #{name}"

Keep value_method on a database column (like the default :id) β€” selected values are re-fetched with Model.where(value_method => ids) to render their labels. With a non-column method, existing selections can't resolve their label; add collection or helper_method as the fallback.

Different options per record​

Pass a lambda instead of a hash β€” it receives the form's record and returns the config (or nil to fall back to helper_method/collection):

form_field :category_id, input_type: :single_select,
fetch_config: ->(record) { { model: "Category", scope_name: record.published? ? "published_categories" : "catalyst_search" } }