From a85c3df3fc1588a8c5188e887c2ab1c609489880 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Tue, 18 Nov 2014 23:48:45 +0100 Subject: [PATCH] README: add polymorphic sort_link info, :scissors: whitespace [skip ci] --- README.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 28596b4..1550bc1 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ If you're coming from MetaSearch, things to note: search object. Instead, you will get your search results (an ActiveRecord::Relation in the case of the ActiveRecord adapter) via a call to `Search#result`. - + 4. If passed `distinct: true`, `result` will generate a `SELECT DISTINCT` to avoid returning duplicate rows, even if conditions on a join would otherwise result in some. @@ -165,6 +165,14 @@ column title or a default sort order: <%= sort_link(@q, :name, 'Last Name', default_order: :desc) %> ``` +With a polymorphic association, you may need to specify the name of the link +explicitly to avoid an `uninitialized constant Model::Xxxable` error (see issue +[#421](https://github.com/activerecord-hackery/ransack/issues/421)): + +```erb +<%= sort_link(@q, :xxxable_of_Ymodel_type_some_attribute, 'Attribute Name') %> +``` + You can also sort on multiple fields by specifying an ordered array: ```erb @@ -389,7 +397,7 @@ class Article < ActiveRecord::Base super else # whitelist only the title and body attributes for other users - super & %w(title body) + super & %w(title body) end end end @@ -404,7 +412,7 @@ class ArticlesController < ApplicationController @q = Article.search(params[:q], auth_object: set_ransack_auth_object) @articles = @q.result end - + private def set_ransack_auth_object @@ -417,13 +425,13 @@ Trying it out in `rails console`: ```ruby > Article -=> Article(id: integer, person_id: integer, title: string, body: text) +=> Article(id: integer, person_id: integer, title: string, body: text) > Article.ransackable_attributes -=> ["title", "body"] +=> ["title", "body"] > Article.ransackable_attributes(:admin) -=> ["id", "person_id", "title", "body"] +=> ["id", "person_id", "title", "body"] > Article.search(id_eq: 1).result.to_sql => SELECT "articles".* FROM "articles" # Note that search param was ignored!