我在使用 mongoid 进行排序时遇到问题。我正在使用 voteable_mongo 收集给定对象的投票,现在我想根据这些进行排序,但我没有任何运气:
它在模型上提供了一个类似这样的字段,并有一个名为 votes_point
的方法:
{"count"=>1, "down"=>[BSON::ObjectId('4f450fd725ae0b7538000002')], "down_count"=>1, "point"=>-1, "up"=>[], "up_count"=>0}
代码:
@word.definitions.order_by([:votes_point]).each do |definition|
或
@word.definitions.order_by([:votes["point"],:desc]).each do |definition|
上述声明并没有根据投票对它们进行任何不同的排序。它的输出与 @word.definitions
我猜这与我试图通过另一个模型的 child 订购这一事实有关?我必须更直接地查询它还是我遗漏了一些东西?
最佳答案
你可以尝试:
@word.definitions.order_by('votes.point')
或
@word.definitions.order_by([['votes.point', :desc]])
关于ruby-on-rails - Rails,对 mongoid 子项进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9484326/