Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rearrangeable ordered list with Ruby on Rails

I want to implement a kind of favorites list. Lets take a play list for this example:

Song A
Song D
Song B
Song C

The order of songs should be saved and the user should be able to rearrange this order and insert new elements at an arbitrary point in the list.

My idea is to use a field "position" and reset it for all elements of the list, when the list is saved. I think that is what Javas Hibernate does.

Another idea is to use position numbers with a big difference, say 1000, so that there is still room for modifications:

Song A 1000
Song D 2000
Song B 3000
Song C 4000

Song C will be inserted between A and D with position number 1500:

Song A 1000
Song C 1500 
Song D 2000
Song B 3000

Any better ideas?

like image 273
deamon Avatar asked Sep 02 '11 11:09

deamon


2 Answers

If you want to roll your own, the answers to this question might be helpful:

Best way to save a ordered List to the Database while keeping the ordering

Seems like the consensus is to just use an order field and re-order them all on save. You can mitigate the amount of database work if you allow the user to do the reordering interactively client side, and just send the final list to save back to the server.

like image 39
spike Avatar answered Oct 23 '22 13:10

spike


Why don't use acts_as_list?

like image 115
lucapette Avatar answered Oct 23 '22 13:10

lucapette