-
-
Notifications
You must be signed in to change notification settings - Fork 829
Sorting on globalized attributes of associations breaks when joining translations #1614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| <!-- This works perfectly for sorting on translated attributes --> | ||
| <%= 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. | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||||||||||||
|
||||||||||||||||||||||
| Book.joins(:translations).ransack({ s: ['category_translations_name asc'] }).result | |
| Book.joins(:translations).ransack({ s: 'category_translations_name asc' }).result |
Copilot
AI
Sep 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Inconsistent use of array syntax for sort parameter. Should use string format for single sort: { s: 'category_translations_name asc' } to match the pattern established in other examples.
Copilot
AI
Sep 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Array syntax is unnecessary for a single sort parameter. Use string format: { s: 'category_translations_name asc' } for consistency.
| 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'] }) | |
| 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' }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The array syntax is not needed for a single sort parameter. Use string format:
{ s: 'category_translations_name asc' }to be consistent with other documentation examples.