Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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 按方法对Rails模型属性排序_Ruby_Sorting_Ruby On Rails 5 - Fatal编程技术网

Ruby 按方法对Rails模型属性排序

Ruby 按方法对Rails模型属性排序,ruby,sorting,ruby-on-rails-5,Ruby,Sorting,Ruby On Rails 5,我有一个使用方法在视图上显示属性的模型。我已经设法把它们分类了,但是它们没有按照我需要的方式分类 顶部有红色的量,这是我想要的,但我需要把绿色和黄色的量颠倒过来。顺序应该是红色,黄色,然后是绿色 以下是将颜色添加到列中的方法: def get_quantity_text_class case when quantity_on_hand > reorder_quantity then 'text-success' when quantity_on_hand > p_lev

我有一个使用方法在视图上显示属性的模型。我已经设法把它们分类了,但是它们没有按照我需要的方式分类

顶部有红色的量,这是我想要的,但我需要把绿色和黄色的量颠倒过来。顺序应该是红色,黄色,然后是绿色

以下是将颜色添加到列中的方法:

def get_quantity_text_class
  case
  when quantity_on_hand > reorder_quantity then 'text-success'
  when quantity_on_hand > p_level then 'text-warning'
  else 'text-danger'
  end
end
def quantity_on_hand
  ppkb.sum(:quantity)
end
下面是创建列的方法:

def get_quantity_text_class
  case
  when quantity_on_hand > reorder_quantity then 'text-success'
  when quantity_on_hand > p_level then 'text-warning'
  else 'text-danger'
  end
end
def quantity_on_hand
  ppkb.sum(:quantity)
end
下面是我使用的排序算法:

sort_by{ |item| item.get_quantity_text_class }

我感觉自己很接近,但我就是不知道如何反转绿色和黄色的数字。

它当前是根据字符串值
文本危险
文本成功
文本警告
排序的。要按所需方式对其排序,请尝试根据数值对其排序:

sort_by do |item| 
  case item.get_quantity_text_class
     when 'text-danger'
       0
     when 'text-warning'
       1
     else
       2
  end
end

它当前根据字符串值
文本危险
文本成功
文本警告
进行排序。要按所需方式对其排序,请尝试根据数值对其排序:

sort_by do |item| 
  case item.get_quantity_text_class
     when 'text-danger'
       0
     when 'text-warning'
       1
     else
       2
  end
end

您可以在项目内定义比较器

class Item
  def <=>(another_item)
    txt_class   = self.get_quantity_text_class
    o_txt_class = another_item.get_quantity_text_class 

    if txt_class == o_txt_class
      # put your logic here for items with identical classes.
      # for example: 
      self.quantity_on_hand <=> another_item.quantity_on_hand
    end

    text_classes = ['text-danger', 'text-warning', 'text-success']

    txt_class_idx   = text_classes.find_index { |e| e == txt_class }
    o_txt_class_idx = text_classes.find_index { |e| e == o_txt_class }

    txt_class_idx <=> o_txt_class_idx
  end
end
类项目
def(另一项)
txt\u class=self.get\u数量\u文本\u类
o_txt_class=另一个_项。获取_数量_文本_class
如果txt\U类==o\U txt\U类
#将您的逻辑放在这里,用于具有相同类的项。
#例如:
self.quantity\u手头上另一个\u项目。quantity\u手头上
结束
text_classes=['text-danger','text-warning','text-success']
txt_class_idx=text_class.find_index{{| e | e==txt_class}
o|txt_class_idx=text_class.find_index{e|e==o|txt_class}
txt_类_idx o_txt_类_idx
结束
结束

然后调用
项。排序

您可以在项中定义比较器

class Item
  def <=>(another_item)
    txt_class   = self.get_quantity_text_class
    o_txt_class = another_item.get_quantity_text_class 

    if txt_class == o_txt_class
      # put your logic here for items with identical classes.
      # for example: 
      self.quantity_on_hand <=> another_item.quantity_on_hand
    end

    text_classes = ['text-danger', 'text-warning', 'text-success']

    txt_class_idx   = text_classes.find_index { |e| e == txt_class }
    o_txt_class_idx = text_classes.find_index { |e| e == o_txt_class }

    txt_class_idx <=> o_txt_class_idx
  end
end
类项目
def(另一项)
txt\u class=self.get\u数量\u文本\u类
o_txt_class=另一个_项。获取_数量_文本_class
如果txt\U类==o\U txt\U类
#将您的逻辑放在这里,用于具有相同类的项。
#例如:
self.quantity\u手头上另一个\u项目。quantity\u手头上
结束
text_classes=['text-danger','text-warning','text-success']
txt_class_idx=text_class.find_index{{| e | e==txt_class}
o|txt_class_idx=text_class.find_index{e|e==o|txt_class}
txt_类_idx o_txt_类_idx
结束
结束
然后调用
项目。排序