diff --git a/docs/docs/getting-started/sorting.md b/docs/docs/getting-started/sorting.md index c0619b1db..1f465119d 100644 --- a/docs/docs/getting-started/sorting.md +++ b/docs/docs/getting-started/sorting.md @@ -69,3 +69,41 @@ class PostsController < ActionController::Base end end ``` + +## Sorting on Association Attributes + +You can sort on attributes of associated models by using the association name followed by the attribute name: + +```ruby +# Sort by the name of the associated category +@q = Post.ransack(s: 'category_name asc') +@posts = @q.result + +# Sort by attributes of nested associations +@q = Post.ransack(s: 'category_section_title desc') +@posts = @q.result +``` + +### Sorting on Globalized/Translated Attributes + +When working with internationalized models (like those using the Globalize gem), special care is needed when sorting on translated attributes of associations. The simplest approach is to use the `sort_link` helper directly with the translation attribute: + +```erb + +<%= sort_link @q, :translations_name %> +<%= sort_link @q, :category_translations_name %> +``` + +For programmatic sorting, let Ransack handle the joins first: + +```ruby +# Let Ransack establish the necessary joins for sorting +@q = Book.ransack(s: 'category_translations_name asc') +@books = @q.result.joins(:translations) + +# For complex scenarios with multiple translations +@q = Book.ransack(s: 'category_translations_name asc') +@books = @q.result.includes(:translations, category: :translations) +``` + +This ensures that Ransack properly handles the join dependencies between your main model's translations and the associated model's translations. diff --git a/docs/docs/going-further/i18n.md b/docs/docs/going-further/i18n.md index 9b20de1d4..c70fa4b06 100644 --- a/docs/docs/going-further/i18n.md +++ b/docs/docs/going-further/i18n.md @@ -51,3 +51,46 @@ en: namespace_article: title: Old Ransack Namespaced Title ``` + +## Working with Globalized Attributes + +If you're using the [Globalize gem](https://github.com/globalize/globalize) for internationalized model attributes, you may encounter issues when sorting on translated attributes of associations while also joining the main model's translations. + +For example, if you have a `Book` model with translated `title` and a `Category` model with translated `name`, sorting on the category's translated name while joining the book's translations may not work as expected: + +```ruby +# This may not work correctly: +Book.joins(:translations).ransack({ s: ['category_translations_name asc'] }).result +``` + +### Workaround for Globalized Attributes Sorting + +When working with globalized attributes and you need to sort on translated fields of associations, the simplest and most effective approach is to use the `sort_link` helper with the translation attribute directly: + +```erb + +<%= sort_link @search, :translations_name %> +<%= sort_link @search, :category_translations_name %> +``` + +For programmatic sorting, let Ransack handle the joins first: + +```ruby +# Instead of joining translations first, let Ransack handle the joins: +search = Book.ransack({ s: ['category_translations_name asc'] }) +results = search.result.joins(:translations) + +# Or use the includes method to ensure all necessary translations are loaded: +search = Book.ransack({ s: ['category_translations_name asc'] }) +results = search.result.includes(:translations, category: :translations) + +# For more complex scenarios, you can manually specify the joins: +search = Book.ransack({ s: ['category_translations_name asc'] }) +results = search.result + .joins(:translations) + .joins(category: :translations) +``` + +The key is to let Ransack establish the necessary joins for sorting first, then add any additional joins you need for the query. + +This approach ensures that Ransack properly handles the complex join dependencies between your main model's translations and the associated model's translations. diff --git a/docs/docs/going-further/other-notes.md b/docs/docs/going-further/other-notes.md index 0937673d6..b67c6aaf5 100644 --- a/docs/docs/going-further/other-notes.md +++ b/docs/docs/going-further/other-notes.md @@ -116,6 +116,39 @@ def index end ``` +### Problem with Globalized Attributes and Sorting + +When using internationalization gems like [Globalize](https://github.com/globalize/globalize), you may encounter issues when trying to sort on translated attributes of associations while also having pre-existing joins to translation tables. + +**Problem scenario:** +```ruby +# This may fail to generate proper joins: +Book.joins(:translations).ransack({ s: ['category_translations_name asc'] }).result +``` + +**Solution:** +The simplest and most effective approach is to use the `sort_link` helper directly with the translation attribute: + +```erb + +<%= sort_link @search, :translations_name %> +<%= sort_link @search, :category_translations_name %> +``` + +For programmatic sorting, let Ransack establish the sorting joins first, then add your additional joins: + +```ruby +# Let Ransack handle the sorting joins first +search = Book.ransack({ s: ['category_translations_name asc'] }) +results = search.result.joins(:translations) + +# Or use includes for complex scenarios +search = Book.ransack({ s: ['category_translations_name asc'] }) +results = search.result.includes(:translations, category: :translations) +``` + +This ensures that Ransack properly handles the join dependencies between your main model's translations and the associated model's translations. + #### `PG::UndefinedFunction: ERROR: could not identify an equality operator for type json` If you get the above error while using `distinct: true` that means that