Make search_form_* helpers honour per-search search_key (#1118) - #1676
Merged
scarroll32 merged 1 commit intoSep 21, 2026
Merged
scarroll32 merged 1 commit into
scarroll32 merged 1 commit into
Conversation
…h search_key (activerecord-hackery#1118) `sort_link` and `sort_url` already read the search key from the search's context (`search.context.search_key`). The form-building helpers were instead falling back to the global `Ransack.options[:search_key]` in `finalize_form_options` / `finalize_form_with_options`, which meant that passing `search_key:` to `Model.ransack` was silently ignored: inputs were emitted under `q[...]` regardless of the override. The two private finalisers now take the `search` object and use `search.context.search_key`. `Context#search_key` already falls back to `Ransack.options[:search_key]` when no per-search override is given, so the global default continues to apply for the unconfigured case.
Member
|
Note Comment from Claude (Claude Code), acting on behalf of @scarroll32. Thanks @ryoya1122. Good catch that Retargeted from Verified locally merged into current Merging with an admin override: the branch predates #1667 so the Rails 8.1 / Ruby 3.3–4.0 required contexts can't report on it; every job it does define is green. |
This was referenced Sep 21, 2026
Open
Merged
scarroll32
added a commit
that referenced
this pull request
Sep 21, 2026
Three changes merged for 4.5.0 had no documentation: enum (#1665) was entirely undocumented. Adds a section showing that an Active Record enum can be searched by label, with eq and in examples, so a select built from Model.enums.keys can be posted back unchanged. Per-search search_key (#1676): the configuration page still told readers to repeat the key as `as: :log_search` in the view, which was the workaround for the bug that PR fixed. The helpers now read it from the search object. Symbol allowlists (#1539): records that ransackable_attributes and ransackable_associations accept symbols or strings interchangeably, and that a symbol list previously matched nothing. Each example was run against the spec schema. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1118.
Problem
When a
search_key:is passed toModel.ransack, the form-building helpers ignore it. The inputs are emitted under the global default key (q[...]) regardless of the override:The same applies to
search_form_withandturbo_search_form_for.sort_linkandsort_urlalready read the key from the search's context, so the form path is the outlier.Cause
The two private finalisers used the global default instead of asking the search:
The reporter linked both lines in #1118.
Fix
finalize_form_options/finalize_form_with_optionsnow take thesearchand readsearch.context.search_key, the same sourcesort_link/sort_urlalready use (seelib/ransack/helpers/form_helper.rb#L200and#L249).Context#search_keyis initialised asoptions[:search_key] || Ransack.options[:search_key], so the global default still applies when no per-search override is given.Compatibility
Model.ransackcallPerson.ransack(...)q[...](global default)q[...](unchanged)Person.ransack(...)afterRansack.configure { ... :example }example[...](global)example[...](unchanged)Person.ransack(..., search_key: :people_search)q[...]❌ (#1118)people_search[...]✓options[:as]/options[:scope]explicitly set by callerThe
finalize_form_*methods areprivate, so the signature change is internal.Tests
Two new specs in
spec/ransack/helpers/form_helper_spec.rb, alongside the existing global-default tests, both red before the fix and green after:#search_form_for with per-search search_key-> expectspeople_search_name_eq#search_form_with with per-search search_key-> expectspeople_search[name_eq]Full suite:
514 examples, 0 failures, 1 pendingonv5.0.0+ this branch (SQLite, ActiveRecord 7.2.3.1, Ruby 3.4.9).Targets
v5.0.0per #1640.