Skip to content
Sergio Cambra edited this page Jan 23, 2025 · 4 revisions

RecordSelect configures through one method with optional arguments, like this:

class UsersController < ApplicationController
  record_select :search_on => [:first_name, last_name]
end

The options you can pass are:

  • :model: the name of the model you want to expose. defaults based on the name of the controller
  • :per_page: how many records to show per page when browsing
  • :notify: a method name to invoke when a record has been selected, if you want server-side notification.
  • :order_by: a SQL string to order the search results
  • :search_on: a field name, or an array of field names. these fields will each be matched against each search term.
  • :full_text_search: a boolean for whether to use a %?% search pattern or not. default is false.
  • :toggle_search_mode: a boolean to add a button to change between full_text_search on (contains) and off (begins).
  • :label: a proc that accepts a record and returns a descriptive string. the default one calls :to_label on the record.
  • :include: as for ActiveRecord::QueryMethods, can load associations used for rendering the record's label.
  • :joins: as for ActiveRecord::QueryMethods, joins with associations so they can used in search_on or order_by.
  • :left_joins: as joins, but does LEFT OUTER JOIN instead of INNER JOIN.

If want to use an association in search_on or order_by, and preload it, add it to both joins/left_joins and include, or define record_select_includes method in the controller, the result of that method will be used as parameter for includes and references, so it's eager loaded with left outer join.

Clone this wiki locally