0

I have a model Post. I want to show first posts where name is 'esa' then order by updated_at.

I tried this in post.rb but doesn't work:

default_scope {Post.where(name: 'esa') && order(updated_at: :desc) }
Nda.S
  • 419
  • 1
  • 6
  • 12
  • 1
    `default_scope` is often discouraged and this use is even more bizarre. Are you positive you want all requests to the `posts` table be filtered and ordered this way? – ndnenkov Dec 19 '16 at 09:02

1 Answers1

0

That would be rather ugly, but here it is.

default_scope do
  order("CASE WHEN name = 'esa' THEN 0 ELSE 1 END").order(updated_at: :desc)
end
Nikita Misharin
  • 1,961
  • 1
  • 11
  • 20