Skip to content
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

Optional distinct added for filtered_collection to sort nested associations #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Mirkoffcito
Copy link

@Mirkoffcito Mirkoffcito commented Aug 22, 2021

Added an optional distinct for sorting nested associations with ransack gem, since you have to set .result(distinct: false) for it to work.

For example, given an endpoint where we have many users, and each user can have many posts, and each post have a title column:

  • /users?q[s]=post_title asc should sort all users based on the title of their posts, but it won't work because of (distinct: true).
    But if we set distinct to false, it will sort properly.

This way, when we want to sort with an association, in out controller we would do:

  • respond_with filtered_collection(User.all, false)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant