`default_scope order('created_at DESC')` works, but `Model.order('created_at DESC')` doesn't work with kaminari

StackOverflow https://stackoverflow.com/questions/21299148

  •  01-10-2022
  •  | 
  •  

문제

I'm using kaminari for pagination in rails.

if I set default_scope order("created_at DESC") in model, it works as expected.

But when I write in controller like this, the DESC is ignored and pages are ordered by ascendent. (In this case I don't write default_scope in model.)

  def index
    @words = Word.order("created_at DESC").page params[:page]
  end

What am I doing wrong?

도움이 되었습니까?

해결책

In this case it applies both orders if want to ignore previous scope then use this:

@words = Word.unscoped.order("created_at DESC").page params[:page]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top