简体   繁体   English

ROR /数据库:如何为数据库记录创建自定义订单列表?

[英]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. 现在在网页上我想使用order_id来订购文章,这完全正常,使用ROR活动记录。

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. 但是,当我想更新order_id时,我必须使用这种技术更新所有记录,每次更改order_id时。 What is a better way of doing this ? 这样做的更好方法是什么?

Thanks 谢谢

You want acts_as_list : 你想要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 . 执行重新排序时无法更新大量记录,但acts_as_list可以使用Article#move_to_topArticle#move_lower等方法为您完成所有这些Article#move_lower

There are some gems to solve your problem. 有一些宝石可以解决你的问题。 The Ruby Toolbox has them in the category Active Record Sortables . Ruby Toolbox将它们放在Active Record Sortables类别中。 As the time of writing (March 2017) the top gems in this list are: 截至撰写时间(2017年3月),此列表中的顶级宝石是:

act_as_list ( Website , GitHub ) act_as_list网站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. 这是管理数据库中有序列表的最受欢迎的选择,它在创建后10年仍然保留。 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. 宝石将以正确的顺序将您的位置字段编号保持为1到n。 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 ) 排名模型GitHub

This gem also manages custom ordered lists for you. 此gem还可以为您管理自定义排序列表。 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. 在我看来,这个宝石可能不再被维护,因为作者现在在做Ember.js开发,它应该可以工作。 Edit: It is still maintained . 编辑: 它仍然保持

sortable ( GitHub ) 可排序的GitHub

This seems to be the same like act_as_list but with the ability to put your items into multiple list. 这似乎与act_as_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 ) 度假村GitHub

This gem uses a linked list approach, ie every database entry gets a pointer to the next entry. 此gem使用链表方法,即每个数据库条目都获得指向下一个条目的指针。 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 ) acts_as_restful_listGitHub

This gem is "Just like acts_as_list, but restful". 这个宝石是“就像acts_as_list,但是很安静”。 It seems to aim for a nicer API. 它似乎旨在获得更好的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. 它背后的公司不再存在,所以我宁愿使用act_as_list并处理它的API,这无论如何都不算​​太糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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