LoginSignup
62
53

More than 5 years have passed since last update.

Active Recordで特定のカラムのユニークなIDの一覧を配列で受け取る

Last updated at Posted at 2013-12-11

例えばチェックイン履歴(UserCheckin)からユニークなuser_idを配列で取得したい時

user_ids = UserCheckin.pluck(:user_id).uniq

[追記]
下記コメントいただきました。SQLレベルでgroup byした方がアプリケーションサイドの実行コストが低くていいですね。ありがとうございます。

user_ids = UserCheckin.group(:user_id).pluck(:user_id)

もしくは

user_ids = UserCheckin.uniq.pluck(:user_id)
62
53
4

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
62
53