ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Confusing sorts behavior

Open peara opened this issue 6 years ago • 3 comments

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.

peara avatar Dec 24 '18 06:12 peara

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

scarroll32 avatar Dec 31 '18 01:12 scarroll32

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

peara avatar Dec 31 '18 05:12 peara