-
-
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'] }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Multiple instances of unnecessary array syntax for single sort parameters. All should use string format: Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
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'] }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Multiple instances of unnecessary array syntax for single sort parameters. All should use string format: Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
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'] }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Multiple instances of unnecessary array syntax for single sort parameters. All should use string format: Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
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 | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The code example uses an array for a single sort parameter. For consistency with other examples in the documentation, consider using a string instead:
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
**Solution:** | ||||||||||||||||||||||
The simplest and most effective approach is to use the `sort_link` helper directly with the translation attribute: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```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 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'] }) | ||||||||||||||||||||||
Comment on lines
+141
to
+142
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||
results = search.result.joins(:translations) | ||||||||||||||||||||||
|
||||||||||||||||||||||
# Or use includes for complex scenarios | ||||||||||||||||||||||
search = Book.ransack({ s: ['category_translations_name asc'] }) | ||||||||||||||||||||||
Comment on lines
+142
to
+146
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||
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 | ||||||||||||||||||||||
|
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.Copilot uses AI. Check for mistakes.