Rails:从控制器排序不起作用

时间:2013-04-20 22:50:46

标签: ruby-on-rails

我想从控制器对数组进行排序,这不起作用,但不会抛出任何错误。

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

评级是一个整数。 附:要显示数组,我使用每种方法。

1 个答案:

答案 0 :(得分:1)

尝试这种结构:

@test = Test.where(:user_id=>current_user.id, :state=>'saved').order('rating')

您可以添加订单方向:

order('rating DESC') or order('rating ASC')