hiyoko-programingの日記

プログラミングを勉強したてのひよっ子。   エンジニア目指して勉強中。

Rails Drill 問題9

必要となる知識

テーブルから取得してきたレコードの塊に対してorderメソッドを利用すると、様々な条件でレコードの順番を並び替えることができる。例えば、order('カラム名 DESC')とすると、指定したレコードのカラムの中身の数値が大きい順にレコードを並び替えてくれる。

コンソール
1
2
3
4
5
6
7
#tweetsテーブルの全てのレコードを取得
pry(main)> tweets = Tweet.all
=> [#<Tweet id: 1, name: "keita", image: "http://image.jpg", text: "rails now. it's so fun!!", created_at: "2015-01-12 07:55:38", updated_at: "2015-01-12 07:55:38">,
 #<Tweet id: 2, name: "satoshi", image: "http://happy.jpg", text: "oh!!happy day!", created_at: "2015-01-12 07:56:21", updated_at: "2015-01-12 11:52:53">・・・]
#取得してきたレコードをidが大きい順に並び替える
pry(main)> tweets.order('id DESC')
=> [#<Tweet id: 2, name: "satoshi", image: "http://happy.jpg", text: "oh!!happy day!", created_at: "2015-01-12 07:56:21", updated_at: "2015-01-12 11:52:53">, #<Tweet id: 1, name: "keita", image: "http://image.jpg", text: "rails now. it's so fun!!", created_at: "2015-01-12 07:55:38", updated_at: "2015-01-12 07:55:38">]

 Tweetsテーブルから全てのレコードを取得し、その順番が生成した時刻を基準に最新順になるように実装する。

実行例

コンソール
1
2
3
4
5
pry(main)> #最新順に並び替えをするコマンド打ってください。
=> [#<Tweet id: 8, name: "keita", image: "http://happy_boy.jpg", text: "i am happy boy!", created_at: "2015-01-12 07:56:21", updated_at: "2015-01-12 07:56:21">,
 #<Tweet id: 7, name: "satoshi", image: "http://happy.jpg", text: "oh!!happy day!", created_at: "2015-01-12 07:56:21", updated_at: "2015-01-12 07:56:21">,
 #<Tweet id: 6, name: "keita", image: "http://image.jpg", text: "rails now. it's so fun!!", created_at: "2015-01-12 07:55:38", updated_at: "2015-01-12 07:55:38">,]
 #pictweetを作成した時に生成したインスタンスも表示されます。

 

解答例
1
2
3
4
5
pry(main)>Tweet.order("created_at DESC")
=> [#<Tweet id: 8, name: "keita", image: "http://happy_boy.jpg", text: "i am happy boy!", created_at: "2015-01-12 11:53:49", updated_at: "2015-01-12 11:53:49">,
 #<Tweet id: 7, name: "satoshi", image: "http://happy.jpg", text: "oh!!happy day!", created_at: "2015-01-12 07:56:21", updated_at: "2015-01-12 11:52:53">,
 #<weet id: 6, name: "keita", image: "http://image.jpg", text: "rails now. it's so fun!!", created_at: "2015-01-12 07:55:38", updated_at: "2015-01-12 07:55:38">]
 インスタンスが生成された時刻が最新順で取得

解説

orderメソッドは,モデル名.order("カラム名")

というように使い、レコードを並び替えて取得することが出来る。
今回は最新順になるように取得するので、インスタンスが保存された時刻を表すカラム、created_atを指定する。

 DESC と ASC

DESC は降順の意味の Descending、

ASC は昇順の意味の Ascending という英語の略。

Tweet.order("created_at DESC")というように使うと降順で、つまり最新順で並び替えをしてくれる。

 

textカラムを指定してorderを実行してみた場合。

日本語が対象だった場合の並び替えの挙動も見られるように、
textカラムの値を日本語にしたインスタンスを新たに2つ保存した。


まずは以下がallの実行結果。

実行例
1
2
3
4
5
6
pry(main)> Tweet.all
=> [#<Tweet id: 6, name: "keita", image: "http://image.jpg", text: "rails now. it's so fun!!", created_at: "2015-01-12 07:55:38", updated_at: "2015-01-12 07:55:38">,
 #<Tweet id: 7, name: "satoshi", image: "http://happy.jpg", text: "oh!!happy day!", created_at: "2015-01-12 07:56:21", updated_at: "2015-01-12 11:52:53">,
 #<Tweet id: 8, name: "keita", image: "http://happy_boy.jpg", text: "i am happy boy!", created_at: "2015-01-12 11:53:49", updated_at: "2015-01-12 11:53:49">,
 #<Tweet id: 10, name: "satoshi", image: "enjoy.jpg", text: "楽しい", created_at: "2015-01-14 11:34:47", updated_at: "2015-01-14 11:34:47",>,
 #<Tweet id: 11, name: "satoshi", image: "enjoy.jpg", text: "ありがとう", created_at: "2015-01-14 11:35:05", updated_at: "2015-01-14 11:35:05">]

この状態でorderを実行。

実行例
1
2
3
4
5
6
pry(main)> Tweet.order("text")
=> [#<Tweet id: 8, name: "keita", image: "http://happy_boy.jpg", text: "i am happy boy!", created_at: "2015-01-12 11:53:49", updated_at: "2015-01-12 11:53:49">,
#<Tweet id: 7, name: "satoshi", image: "http://happy.jpg", text: "oh!!happy day!", created_at: "2015-01-12 07:56:21", updated_at: "2015-01-12 11:52:53">,
 #<Tweet id: 6, name: "keita", image: "http://image.jpg", text: "rails now. it's so fun!!", created_at: "2015-01-12 07:55:38", updated_at: "2015-01-12 07:55:38">,
 #<Tweet id: 11, name: "satoshi", image: "enjoy.jpg", text: "ありがとう", created_at: "2015-01-14 11:35:05", updated_at: "2015-01-14 11:35:05">,
 #<Tweet id: 10, name: "satoshi", image: "enjoy.jpg", text: "楽しい", created_at: "2015-01-14 11:34:47", updated_at: "2015-01-14 11:34:47">]

最初の3つのレコードはtextカラムの値がアルファベット順に、最後の2つは五十音順に並び替えられた。