Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 24, 2025

  • Analyzed the issue with globalized attributes and Ransack sorting
  • Identified the root cause: When pre-existing joins exist to translations, Ransack fails to add joins to association's translation tables
  • Located relevant code in lib/ransack/adapters/active_record/context.rb
  • Create documentation fix showing the workaround
  • Add documentation example for proper handling of globalized attributes sorting
  • Test the documented solution works correctly

Additional instructions:

Please fix as a docs update:
I faced same issue with sorting. this work for me perfectly:
<%= sort_link @search, :translations_name %>

Fixes #965


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: scarroll32 <11340230+scarroll32@users.noreply.github.com>
@scarroll32 scarroll32 requested review from Copilot and removed request for scarroll32 September 24, 2025 21:26
@scarroll32 scarroll32 marked this pull request as ready for review September 24, 2025 21:26
@scarroll32 scarroll32 enabled auto-merge (squash) September 24, 2025 21:26
@scarroll32 scarroll32 changed the title [WIP] Sorting on globalized attributes of associations breaks when joining translations Sorting on globalized attributes of associations breaks when joining translations Sep 24, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses an issue with sorting on globalized attributes of associations when pre-existing joins to translation tables exist. The solution involves documenting workarounds to help users properly handle globalized attributes sorting with Ransack.

  • Documents the issue with globalized attributes and Ransack sorting when translation joins are pre-established
  • Provides workarounds using sort_link helper and proper join ordering
  • Adds examples across multiple documentation files for comprehensive coverage

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
docs/docs/going-further/other-notes.md Adds troubleshooting section for globalized attributes sorting issues
docs/docs/going-further/i18n.md Expands i18n documentation with globalized attributes workarounds
docs/docs/getting-started/sorting.md Adds association sorting examples and globalized attributes guidance

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

**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.

Comment on lines +141 to +142
# Let Ransack handle the sorting joins first
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] 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.

Comment on lines +142 to +146
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'] })
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.


```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.


```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.

@scarroll32 scarroll32 merged commit 103b7f6 into main Sep 24, 2025
25 checks passed
@scarroll32 scarroll32 deleted the copilot/fix-42cc1e49-53a3-4c41-91c2-f3cc9417e2ce branch September 24, 2025 21:28
scarroll32 added a commit that referenced this pull request Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sorting on globalized attributes of associations breaks when joining translations
2 participants