activerecord-hackery / ransack

Object-based searching.
https://activerecord-hackery.github.io/ransack/
MIT License
5.69k stars 806 forks source link

Confusing sorts behavior #994

Open peara opened 5 years ago

peara commented 5 years ago

Current sorts=: https://github.com/activerecord-hackery/ransack/blob/6bcfcc12c5e0f78b014afe47ad9de09ed5745272/lib/ransack/search.rb#L54:L76

This allow very confusing codes such as:

users = User.ransack()
users.sorts = 'id asc'
users.sorts = 'name asc'

puts users.sorts.length # 2

In wiki as well as tests, I saw no mention of such feature. It also prohibit a clean way of reset sorts by reassign it. What do you think? I would expect it to change to allow a sort reset feature and disallow confusing code.

scarroll32 commented 5 years ago

Hi @peara thanks for your contribution. What do you mean by resetting / reassigning sorts?

It does need to be documented and tested, but it is essentially passing through a regular ActiveRecord sort, which then translates directly to a SQL order by

peara commented 5 years ago

@seanfcarroll Thank you for your reply. I mean when you already assign a sort parameters, if for some reasons, you want to assign it another one, the only way now is to create a new ransack. Because sorts= will just add more parameters. I think it's should be << as it is more align with current behaviour. And = should remove previous sorts before adding new one.

silent-e commented 6 months ago

I am confused by this as well. Since sorts is an array internally I'd have assumed that we should use << to append a new entry and assigning via = would create a brand new sorting context. However sorts= internally is using << to append to the sorts array.

I'm trying to cycle through a sorting context. First time ascending, second click descending, third click no sorting at all. However I can't figure out how to, when using the sort_link helper which I believe we absolutely need to do, unset the sorting context for that third click.

You can't even use search.sorts = [] to reset to no sorting.