简体   繁体   中英

ROR / Database: How to create a custom order list for database records?

I have a database table with some articles (for a website) like so:

Articles:
id       title         order_id
1          -            1
2          -            4
3          -            3
4          -            2

Now on the webpage I want to use the order_id to order the articles, this works perfectly fine, using ROR active record.

However when I want to update the order_id I would have to update all of the records using this technique, each time a change to the order_id is made. What is a better way of doing this ?

Thanks

You want acts_as_list :

class Article < ActiveRecord::Base
  acts_as_list :column => 'order_id'
end

There's no way around updating lots of records when you perform a reordering, but acts_as_list can do all that for you with methods like Article#move_to_top and Article#move_lower .

There are some gems to solve your problem. The Ruby Toolbox has them in the category Active Record Sortables . As the time of writing (March 2017) the top gems in this list are:

act_as_list ( Website , GitHub )

This is the most popular choice for managing an ordered list in the database and it is still maintained 10 years after its creation. It will do just what you wanted and manage the numbers of the items. The gem will keep your position field numbers form 1 to n in the correct order. This however means that inserting items in the middle of the list means increasing all of the position values for the list items below it, which can be quite some work for your database.

ranked-model ( GitHub )

This gem also manages custom ordered lists for you. However it uses another approach behind the scenes, where your list items get position numbers big and spaced apart across the full range of integer values. This should get you performance benefits if you have large lists and need to reorder the items often. It seems to me that this gem might no longer be maintained though, since the author is now doing Ember.js development, it should work though. Edit: It is still maintained .

sortable ( GitHub )

This seems to be the same like act_as_list but with the ability to put your items into multiple list. I'm not really sure if this is a valid use-case since you could just create multiple items. It looks like it was not maintained for a long time and not used by many.

resort ( GitHub )

This gem uses a linked list approach, ie every database entry gets a pointer to the next entry. This might be a good idea if you need a lot of inserts in the middle of your lists, but seems like a terrible idea for just getting the list of entires or if something goes wrong in the database and the chain breaks. It is quite new, so let's see how it develops.

acts_as_restful_list ( GitHub )

This gem is "Just like acts_as_list, but restful". It seems to aim for a nicer API. The company behind it does no longer exist, so I'd rather use act_as_list and deal with its API, which is not too bad anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM