我想从控制器对数组进行排序,这不起作用,但不会抛出任何错误。
def my_published
@tests=Test.where(:user_id => current_user.id, :state=>'saved')
@tests=@tests.sort { |p1, p2| p1.rating <=> p2.rating }
respond_to do |format|
format.html
format.js{@tests}
end
end
评级是一个整数。 附:要显示数组,我使用每种方法。
答案 0 :(得分:1)
尝试这种结构:
@test = Test.where(:user_id=>current_user.id, :state=>'saved').order('rating')
您可以添加订单方向:
order('rating DESC') or order('rating ASC')