简体   繁体   English

如何在Rails中保留关联记录的顺序?

[英]How to preserve the order of associated records in Rails?

I am trying to work out an interface that allows user to rearrange the records on the page and make sure that when they save the order is preserved. 我正在尝试设计一个界面,允许用户重新排列页面上的记录,并确保保存订单时保留。 I approached it first by creating a field for order 我首先通过创建订单字段来接近它

class Node
  field :order, :type=>Integer
end

node_ids = ["42dw23ffe3", "56dfsa2edfsd"]

But I run into a wall trying to make is work in an efficient way. 但是,我试图以一种有效的方式努力工作。 The method I think of requires making twice as many queries as there are nodes -- looking up and updating. 我想到的方法要求查询的次数是节点的两倍 - 查找和更新。

Is there a better way of keeping a set of nodes in an arbitrary, user-specified order? 有没有更好的方法将一组节点保持在用户指定的任意顺序中?

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

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