Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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:ActiveAdmin按索引排序,然后按自定义方法值排序_Ruby On Rails_Sorting_Activerecord_Activeadmin - Fatal编程技术网

Ruby on rails Rails:ActiveAdmin按索引排序,然后按自定义方法值排序

Ruby on rails Rails:ActiveAdmin按索引排序,然后按自定义方法值排序,ruby-on-rails,sorting,activerecord,activeadmin,Ruby On Rails,Sorting,Activerecord,Activeadmin,因此,我有以下几点 Dialogue.rb belongs_to Chapter # Get the total number of dialogues in a chapter, then see what number we are def order_in_chapter chapter.dialogues.index(self) + 1 end Chapter.rb has_many Stories 章节也有一个“订单号”,所以我们知道章节的显示顺序 问题是,在活动管

因此,我有以下几点

Dialogue.rb
 belongs_to Chapter
 # Get the total number of dialogues in a chapter, then see what number we are
 def order_in_chapter
   chapter.dialogues.index(self) + 1
 end

Chapter.rb
 has_many Stories
章节也有一个“订单号”,所以我们知道章节的显示顺序

问题是,在活动管理上,业务需求是按章节排序,然后是对话编号。按多个索引排序并不太困难,但按一个索引排序,然后按方法的返回值排序返回的关联似乎是不可能的

供参考:

ActiveAdmin.register Dialogue do
  controller do
    def scoped_collection
      super.includes :chapter, :character
    end
  end
  index do
    column :chapter, sortable: "chapters.order_num"
  end
这将正确地对章节进行排序(第1章、第2章、第3章等),但是当我希望通过dialogue.order_in_chapter方法返回这些章节中的对话时,这些章节中的对话将以混乱的顺序返回

有办法做到这一点吗

我需要的伪代码如下所示:

StoryVoice.order(chapter: :asc).then_by(order_in_chapter: :asc)

您不能按ruby方法结果进行排序,只能通过sql进行排序