25
27

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.

ActiveRecord::Relationから配列へ(pluck)

Posted at

特定のレコードのカラムで配列を作る

member.rb
member = Member.select(:id) #=> [#<Member id: 1>, #<Member id: 2>, #<Member id: 3>, #<Member id: 4>, #<Member id: 5>]

という感じでActiveRecord::Relationクラスが返ってくるけど、配列が欲しかったのでmap使ってた。
けど、"pluck"とかあったのか。

member.rb
member = Member.pluck(:id) #=> [1, 2, 3, 4, 5]
25
27
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
25
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?