Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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 如何使用ransack gem按模型方法排序_Ruby On Rails_Ruby_Associations_Arel_Ransack - Fatal编程技术网

Ruby on rails 如何使用ransack gem按模型方法排序

Ruby on rails 如何使用ransack gem按模型方法排序,ruby-on-rails,ruby,associations,arel,ransack,Ruby On Rails,Ruby,Associations,Arel,Ransack,我已经在我的rails应用程序中实现了Ransack gem,我正在使用它在表上搜索/排序。然而,在一些方面,我需要按模型方法返回的整数进行排序 根据我所阅读和研究的内容,为了在Ransack中实现这一点,我必须创建一个ransacker方法,基本上将模型方法转换为Arel。我没有Arel的经验,但一直在研究它 有谁能帮我弄清楚我需要使用什么Arel,或者如何最好地创建一个能满足我需要的搜掠器 型号: def highest_mileage high_mileage =

我已经在我的rails应用程序中实现了Ransack gem,我正在使用它在表上搜索/排序。然而,在一些方面,我需要按模型方法返回的整数进行排序

根据我所阅读和研究的内容,为了在Ransack中实现这一点,我必须创建一个ransacker方法,基本上将模型方法转换为Arel。我没有Arel的经验,但一直在研究它

有谁能帮我弄清楚我需要使用什么Arel,或者如何最好地创建一个能满足我需要的搜掠器

型号:

    def highest_mileage
        high_mileage = Expense.where("vehicle_id = ? and vehicle_mileage IS NOT NULL", self.id).maximum("vehicle_mileage")
        mileage = (high_mileage.nil? ? 0 : high_mileage)
        return mileage
    end
    %table
      %thead
        %tr
          %th = sort_link(@q, :highest_mileage)
Index.html.haml:

    def highest_mileage
        high_mileage = Expense.where("vehicle_id = ? and vehicle_mileage IS NOT NULL", self.id).maximum("vehicle_mileage")
        mileage = (high_mileage.nil? ? 0 : high_mileage)
        return mileage
    end
    %table
      %thead
        %tr
          %th = sort_link(@q, :highest_mileage)

我尝试了两种不同的方法来实现一个定制的洗劫器,但是由于我缺乏Arel知识,所以我做不到。下面是我试图复制它的最新成果

    ransacker :highest_mileage do |parent|
      expense_item = ExpenseItem.arel_table
      a = expense_item.project(expense_item[Arel.star]).where(expense_item[:vehicle_id].eq(parent.table[:vehicle_id]))
代码尚未完成,因为我不确定如何在Arel中执行下一步。老实说,我确信我的方法也不正确

感谢您的帮助