回答編集履歴

1

追記

2016/02/04 10:12

投稿

katoy
katoy

スコア22324

test CHANGED
@@ -91,3 +91,59 @@
91
91
  > Client.maximum("age")
92
92
 
93
93
  > ...
94
+
95
+
96
+
97
+ 追記: 2016-02-04
98
+
99
+ created_at で desc でsort して、limit をとったら、ruby 側で目的とする項目でソートするのは如何でしょう。
100
+
101
+ ```
102
+
103
+ Frame number: 0/20
104
+
105
+ [1] pry(main)> xx = Client.order("created_at desc").limit(6)
106
+
107
+ Client Load (130.5ms) SELECT "clients".* FROM "clients" ORDER BY created_at desc LIMIT 6
108
+
109
+ +----+------------+-----------+--------+--------------+-----------------+-----------------+
110
+
111
+ | id | first_name | last_name | locked | orders_count | created_at | updated_at |
112
+
113
+ +----+------------+-----------+--------+--------------+-----------------+-----------------+
114
+
115
+ | 12 | Nick | | false | | 2015-12-27 0... | 2015-12-27 0... |
116
+
117
+ | 11 | AndyY | | true | | 2015-12-27 0... | 2015-12-27 0... |
118
+
119
+ | 10 | AndyX | | true | | 2015-12-27 0... | 2015-12-27 0... |
120
+
121
+ | 9 | Andy | Smith | false | | 2015-12-27 0... | 2015-12-27 0... |
122
+
123
+ | 8 | ussel | Smith | false | | 2015-12-27 0... | 2015-12-27 0... |
124
+
125
+ | 7 | 安倍 | 次郎 | true | 3 | 2015-12-27 0... | 2015-12-27 0... |
126
+
127
+ +----+------------+-----------+--------+--------------+-----------------+-----------------+
128
+
129
+ 6 rows in set
130
+
131
+ [2] pry(main)> xx.to_a.sort{|a,b| a.orders_count.to_i <=> b.orders_count.to_i}.reverse[0]
132
+
133
+ +----+------------+-----------+--------+--------------+-----------------+-----------------+
134
+
135
+ | id | first_name | last_name | locked | orders_count | created_at | updated_at |
136
+
137
+ +----+------------+-----------+--------+--------------+-----------------+-----------------+
138
+
139
+ | 7 | 安倍 | 次郎 | true | 3 | 2015-12-27 0... | 2015-12-27 0... |
140
+
141
+ +----+------------+-----------+--------+--------------+-----------------+-----------------+
142
+
143
+ 1 row in set
144
+
145
+ [3] pry(main)>
146
+
147
+ ```
148
+
149
+