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?
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.
Why don't use acts_as_list?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With