Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails中对选项组的子项进行排序_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 如何在Rails中对选项组的子项进行排序

Ruby on rails 如何在Rails中对选项组的子项进行排序,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我有一个选择列表,其中包含从option\u groups\u从\u collection\u为\u select提供的optgroup和子选项 这很好,但我看不到任何方法可以在任何文档中指定每个子节的排序顺序。我宁愿不必自己绘制地图和分类;我认为这将是一个足够普遍的需要成为助手的一部分 如何对每个分组中的子项进行排序?在您的中声明一个顺序。以api文档中的示例为例,您可以执行以下操作: class Continent < ActiveRecord::Base has_many :co

我有一个选择列表,其中包含从
option\u groups\u从\u collection\u为\u select提供的optgroup和子选项

这很好,但我看不到任何方法可以在任何文档中指定每个子节的排序顺序。我宁愿不必自己绘制地图和分类;我认为这将是一个足够普遍的需要成为助手的一部分


如何对每个分组中的子项进行排序?

在您的
中声明一个顺序。以api文档中的示例为例,您可以执行以下操作:

class Continent < ActiveRecord::Base
  has_many :countries, -> { order("countries.name") }
end

class Country < ActiveRecord::Base
  belongs_to :continent
end
class{order(“countries.name”)}
结束
类国家/地区

如果您希望在集合中使用专用数据,请选择通常最容易在模型中处理的数据。

我不建议使用默认排序,因为如果您需要其他数据,您需要重新排序。
。@schmijos通常我也会,但是当使用
选项\u collection\u中的\u groups\u作为\u select时
没有其他方式获得订单。