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

时间:2021-12-08 22:13:51

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. What is a better way of doing this ?

但是,当我想要更新order_id时,我必须使用这种技术更新所有记录,每次对order_id进行更改时。还有什么更好的办法呢?

Thanks

谢谢

2 个解决方案

#1


3  

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可以使用#move_to_top和#move_lower这样的方法为您完成所有这些工作。

#2


0  

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:

有一些宝石可以解决你的问题。Ruby工具箱在“活动记录分类表”类别中有它们。截止写作时间(2017年3月),本表中最重要的宝石是:

act_as_list (Website, GitHub)

GitHub act_as_list(网站)

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.

这是管理数据库中有序列表的最流行的选择,它在创建10年后仍然被维护。它将做你想做的,并管理项目的数量。宝石将保持你的位置字段编号从1到n的正确顺序。然而,这意味着在列表中间插入项目意味着增加列表项下面的所有位置值,这对于数据库来说是相当困难的工作。

ranked-model (GitHub)

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.

此gem还为您管理自定义有序列表。然而,它在幕后使用了另一种方法,在这种方法中,您的列表项在整数值的整个范围内获得较大的位置值,并且间隔开来。如果您有大的列表,并且需要经常重新排列项目,那么这将使您的性能受益。在我看来,这个宝石可能不再被维护,因为作者现在正在做烬。js开发应该可以工作。编辑:它仍然被维护。

sortable (GitHub)

可分类的(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.

这似乎与act_as_list类似,但是可以将项目放入多个列表。我不确定这是否是一个有效的用例,因为您可以创建多个项目。看起来它没有维护很长时间,也没有被很多人使用。

resort (GitHub)

度假村(GitHub)

This gem uses a linked list approach, i.e. 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.

这个gem使用了一个链表方法,即每个数据库条目都有一个指向下一个条目的指针。如果在列表中间需要大量插入,这可能是一个好主意,但是仅仅获取实体列表似乎是一个糟糕的想法,或者如果数据库中出现问题并且链中断了。它是相当新的,让我们看看它是如何发展的。

acts_as_restful_list (GitHub)

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.

这个gem“就像acts_as_list,但是restful”。它似乎是为了一个更好的API。它背后的公司已经不存在了,所以我宁愿使用act_as_list来处理它的API,无论如何这也不算太糟。

#1


3  

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可以使用#move_to_top和#move_lower这样的方法为您完成所有这些工作。

#2


0  

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:

有一些宝石可以解决你的问题。Ruby工具箱在“活动记录分类表”类别中有它们。截止写作时间(2017年3月),本表中最重要的宝石是:

act_as_list (Website, GitHub)

GitHub act_as_list(网站)

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.

这是管理数据库中有序列表的最流行的选择,它在创建10年后仍然被维护。它将做你想做的,并管理项目的数量。宝石将保持你的位置字段编号从1到n的正确顺序。然而,这意味着在列表中间插入项目意味着增加列表项下面的所有位置值,这对于数据库来说是相当困难的工作。

ranked-model (GitHub)

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.

此gem还为您管理自定义有序列表。然而,它在幕后使用了另一种方法,在这种方法中,您的列表项在整数值的整个范围内获得较大的位置值,并且间隔开来。如果您有大的列表,并且需要经常重新排列项目,那么这将使您的性能受益。在我看来,这个宝石可能不再被维护,因为作者现在正在做烬。js开发应该可以工作。编辑:它仍然被维护。

sortable (GitHub)

可分类的(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.

这似乎与act_as_list类似,但是可以将项目放入多个列表。我不确定这是否是一个有效的用例,因为您可以创建多个项目。看起来它没有维护很长时间,也没有被很多人使用。

resort (GitHub)

度假村(GitHub)

This gem uses a linked list approach, i.e. 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.

这个gem使用了一个链表方法,即每个数据库条目都有一个指向下一个条目的指针。如果在列表中间需要大量插入,这可能是一个好主意,但是仅仅获取实体列表似乎是一个糟糕的想法,或者如果数据库中出现问题并且链中断了。它是相当新的,让我们看看它是如何发展的。

acts_as_restful_list (GitHub)

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.

这个gem“就像acts_as_list,但是restful”。它似乎是为了一个更好的API。它背后的公司已经不存在了,所以我宁愿使用act_as_list来处理它的API,无论如何这也不算太糟。