`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
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top