複数カラムから2次元的な配列としても取り出せる。flattenすれば1元配列として扱えて、今回はこれを使うかも。
[57] pry(main)> Prize.pluck(:grade, :sales_quantity).first(10)
(10.2ms) SELECT "prizes"."grade", "prizes"."sales_quantity" FROM "prizes"
=> [[1, 1], [2, 9], [3, 118], [4, 5961], [5, 99657], [6, 179019], [1, 6], [2, 66], [3, 1719], [4, 48235]]
[58] pry(main)> Prize.pluck(:grade, :sales_quantity).first(10).flatten
(10.3ms) SELECT "prizes"."grade", "prizes"."sales_quantity" FROM "prizes"
=> [1, 1, 2, 9, 3, 118, 4, 5961, 5, 99657, 6, 179019, 1, 6, 2, 66, 3, 1719, 4, 48235]
[59] pry(main)> a = Prize.pluck(:grade, :sales_quantity).first(10)
(10.5ms) SELECT "prizes"."grade", "prizes"."sales_quantity" FROM "prizes"
=> [[1, 1], [2, 9], [3, 118], [4, 5961], [5, 99657], [6, 179019], [1, 6], [2, 66], [3, 1719], [4, 48235]]
[60] pry(main)> a[1]
=> [2, 9]
[61] pry(main)> a[1][1]
=> 9
0 件のコメント:
コメントを投稿