简体   繁体   中英

Rails:sorting from controller doesn't work

I want to sort array from controller, that doesn't works, but throws no errors.

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

Rating is an integer. PS To display array I use each method.

Try this construction:

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

You can add the direction of order:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM