Reputation: 1779
I'm having a problem sorting with mongoid. I'm using voteable_mongo to collect votes on a given object and now I'd like to order based on those but I'm not having any luck:
It gives a field like so on a model and has a method called votes_point
:
{"count"=>1, "down"=>[BSON::ObjectId('4f450fd725ae0b7538000002')], "down_count"=>1, "point"=>-1, "up"=>[], "up_count"=>0}
Code:
@word.definitions.order_by([:votes_point]).each do |definition|
or
@word.definitions.order_by([:votes["point"],:desc]).each do |definition|
The above statement doesn't order them any differently based on the votes. It outputs exactly the same as @word.definitions
I'm guessing it has something to do with the fact I'm trying to order by the children of another model? Must I query it more directly or am I missing something?
Upvotes: 2
Views: 958
Reputation: 56
You might try:
@word.definitions.order_by('votes.point')
or
@word.definitions.order_by([['votes.point', :desc]])
Upvotes: 2