Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/docs/getting-started/sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<!-- This works perfectly for sorting on translated attributes -->
<%= 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.
43 changes: 43 additions & 0 deletions docs/docs/going-further/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Preview

Copilot AI Sep 24, 2025

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.

```

### 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'] })
Copy link
Preview

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The 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: { s: 'category_translations_name asc' } for consistency.

Copilot uses AI. Check for mistakes.

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'] })
Copy link
Preview

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The 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: { s: 'category_translations_name asc' } for consistency.

Copilot uses AI. Check for mistakes.

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'] })
Copy link
Preview

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The 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: { s: 'category_translations_name asc' } for consistency.

Copilot uses AI. Check for mistakes.

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.
33 changes: 33 additions & 0 deletions docs/docs/going-further/other-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Preview

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The 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: { s: 'category_translations_name asc' }

Suggested change
Book.joins(:translations).ransack({ s: ['category_translations_name asc'] }).result
Book.joins(:translations).ransack({ s: 'category_translations_name asc' }).result

Copilot uses AI. Check for mistakes.

```

**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
Copy link
Preview

Copilot AI Sep 24, 2025

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 uses AI. Check for mistakes.

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
Copy link
Preview

Copilot AI Sep 24, 2025

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.

Suggested change
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' })

Copilot uses AI. Check for mistakes.

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
Expand Down
Loading