1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Rails頭の体操】これは何と同じ動作でしょう?

Last updated at Posted at 2017-06-16

問題

今やってるプロジェクトにポツンとあった不思議なスコープからのクイズです。:chicken:

categories = Category.first(3)

このcategoriesを以下のスコープが並び替えます。

Category.rb
scope :order_array_by_updated, ->(categories_array){
  where(id: categorie_array.map(&:id)).
  order("updated_at desc")
}

このスコープはrailsで用意されているメソッドを使えばもっと簡単に書けます。
それはなんでしょうか?



ヒント

以下のように呼んで、
categoriesをupdated_at降順に並び替えてくれます。

Category.order_array_by_updated(categories)















正解

categories.order("updated_at desc")

何かに疲れた誰かが定義してしまったのでしょう:innocent:

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?