In my controller I have:
if params[:sort].nil?
@sort = "created_at"
else
@sort = params[:sort]
end
@konkurrencer = Konkurrencer.where("id NOT IN(?)", @clicked).order("#{@sort} DESC")
I like to add if params[:sort] is different from "created_at", "ratings", or "rating" then it should sort after "created_at".
First,
.order("#{@sort} DESC")isn’t a good idea when@sortis taken straight from params. It would be better to use.order('? DESC', @sort).http://guides.rubyonrails.org/security.html#sql-injection
I’m not sure if I read your question correctly but I’m assuming you want
created_atto be the default order with the other valid options beingratingsandrating.