0
0

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 3 years have passed since last update.

Rails:pluckを使う必要性

Posted at

表記例

current_user.followings.pluck(:id)
# =>[1,2,3]

.plackは指定した引数の値をカラムから、配列で返してくれるメソッド

必要なデータだけを取れるのであればmapメソッドもあるが
何故pluckメソッドを使用するのか?

理由

巨大なコレクションを操作する場合、
mapメソッドでは読み込んだ行を一行づつオブジェクトに変換しているが、使用するフィールドは全ては使用はしません。

必要なフィールドだけを読み込むことによって
・スピード
・メモリ使用量
の効率化が図ることができるようになる

注意

pluckメソッドが返す値は配列を返してくれる

plackメソッド使用後、モデルの更新などを行いたい場合は使用することができないので注意が必要

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?