What is the most efficient way to allow users to order a model?
I have a note
model. A user
has many notes
. At the moment, the notes
are ordered by date
, and then by created_at
.
I would like to give users the ability to order the notes (within a date) themselves.
For instance, if this is how they are currently displayed:
Date1
note1,note2,note3
Date2
note4
Date3
note5,note6
The user could reorder them as follows:
Date1
note3,note1,note2
开发者_运维百科Date2
note4
Date3
note6,note5
What is the most efficient way to implement this in Rails? Should I used a linked list? Or something else?
I'm using Rails 3.0.1 and Ruby 1.9.2
My preference is to do this directly in the Model and let the database handle the ordering so I usually add a order_by field. When the user reorders the list the code simply tweaks the order_by field to reflect the new ordering.
I think that i would give it a position attribute. Thus, initially :
note1 -> 1 note2 -> 2 note3 -> 3 note4 -> 4 note5 -> 5
If i want note5 to get a position of 2, i would just need to exchange the position attributes.
精彩评论