From 332ae222a8226b82ce8a1048b0419a0eabd33361 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Sat, 19 Dec 2015 20:23:02 -0500 Subject: [PATCH] Document the use of ransack_alias --- CHANGELOG.md | 6 ++++++ README.md | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbc7d59..d34fd0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,12 @@ ### Added +* Added `ransack_alias` to allow users to customize the names for long + ransack field names. PR + [#623](https://github.com/activerecord-hackery/ransack/pull/623), commit [e712ff1](https://github.com/activerecord-hackery/ransack/commit/e712ff1). + + *Ray Zane* + * Added support for searching on attributes that have been added to Active Record models with `alias_attribute` (Rails >= 4 only). PR [#592](https://github.com/activerecord-hackery/ransack/pull/592), commit diff --git a/README.md b/README.md index 48d86cf..46d7bd9 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,27 @@ If you have trouble sorting on associations, try using an SQL string with the pluralized table (`'departments.title'`,`'employees.last_name'`) instead of the symbolized association (`:department_title)`, `:employees_last_name`). +### Ransack Aliases + +You can customize the attribute names for your Ransack searches by using a `ransack_alias`. This is particularly useful for long attribute names that are necessary when querying associations or multiple columns. + +```ruby +class Post < ActiveRecord::Base + belongs_to :author + + # Abbreviate :author_first_name_or_author_last_name to :author + ransack_alias :author, :author_first_name_or_author_last_name +end +``` + +Now, rather than using `:author_first_name_or_author_last_name_cont` in your form, you can simply use `:author_cont`. This serves to produce more expressive query parameters in your URLs. + +```erb +<%= search_form_for @q do |f| %> + <%= f.label :author_cont %> + <%= f.search_field :author_cont %> +<% end %> +``` ### Using Ransackers to add custom search functions via Arel