Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.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 如何按属于mongoid中另一个模型的模型中的字段排序?_Ruby On Rails_Mongodb_Mongoid - Fatal编程技术网

Ruby on rails 如何按属于mongoid中另一个模型的模型中的字段排序?

Ruby on rails 如何按属于mongoid中另一个模型的模型中的字段排序?,ruby-on-rails,mongodb,mongoid,Ruby On Rails,Mongodb,Mongoid,所以我有这些模型: class b :field boolean, :type => Boolean end class c embeds_many :a end class a belongs_to :b scope :sort_by_boolean, order_by(:b.boolean => :asc) end 我试过那样做,但不可能。还有别的办法点这个吗? 我能想到的另一件事是循环遍历它,创建两个不同的数组,一个布尔值为真,另一个为假,并将两者结合起来

所以我有这些模型:

class b
  :field boolean, :type => Boolean
end

class c
  embeds_many :a
end

class a
  belongs_to :b
  scope :sort_by_boolean, order_by(:b.boolean => :asc)
end
我试过那样做,但不可能。还有别的办法点这个吗?
我能想到的另一件事是循环遍历它,创建两个不同的数组,一个布尔值为真,另一个为假,并将两者结合起来。但是有没有更简单的方法呢?

我最终还是以循环的方式完成了,但是如果有更简单的方法,我更愿意:

class c
  def sort
    not_true = []
    is_true = []
    self.a.each { |x|
      if x.b.boolean
        is_true.push x
      else
        not_true.push x
      end
    }
    not_true + is_true
  end
end