brendon / ranked-model

An acts_as_sortable/acts_as_list replacement built for Rails 4+
https://github.com/mixonic/ranked-model
MIT License
1.09k stars 134 forks source link

wrong attribute update #86

Closed tommybernaciak closed 4 months ago

tommybernaciak commented 10 years ago

This is very nice gem, but has a liitle bug. Attribute used for ranking is wrongly updated. I found this when implementing some sortable tables in rails. List of users is ordered by position - attribute use by ranked-model. Here is how the table look like: table1

After taking element with position 15 to 5 every element gets updated, but look what happened to element with position > 15, it gets also added 1 so now there is a break (17th element after 15th, no element with position 16). table2 After some modification of order it gets really annoying, because you dont know exactly how its going to work.

Another mistake is that it doesnt work for two elements at all. Position is getting max integer and min integer value.

I am using Postgres with Rails 4, if you want to see my implementation I can add link to the project.

mixonic commented 10 years ago

@tommy-dev ranked-model gets its efficiency gain by not managing position, but instead only providing relative rankings in a large integer space. There is no guarantee there will not be a gap in the ranking.

This should be spelled out pretty clearly at the top of the Readme. It seems here that your ranking column (ranks, not positions) is very tightly clustered. That means a rank rebalance is happening on every change. This is definitely not optimal, maybe this is coming from old data in a position column?

Also, please upload an image without emails addresses, names and phone number.

tommybernaciak commented 10 years ago

Here is my implementation, based on the readme: https://github.com/binarapps/sortable-tables Position is a field used by ranked-model (ranks :position in user model) My idea was to use this field as a position in the table, so after changing one elements position, other gonna be updated, and the order will be kept. The problem with update is that it doesnt take under consideration the previous position of changed element. In the posted pics you can see that in all elements after 5 position is added, that why there is a gap here. If it is not going to work like this, please tell me so I will implement sth on my own. I just thought it is good gem for this task, but maybe I didnt understand how it is working.

And dont worry about the images, data is Faker generated so no real personal data is shown here. :)

mixonic commented 10 years ago

@tommy-dev I remain very uncertain as to what you are trying to do.

ranked models stores a rank in the database. You should not manually be setting the rank. It provides a position attribute (explained on the readme) for reading the position, but this is merely a convenience.

If something is awry, a test or example code would help me understand what you are expecting to happen.

brendon commented 4 months ago

There was a bug that may have caused this. It was fixed a long time ago.