LoginSignup
0
0

More than 1 year has passed since last update.

uniqとは?distinctとの違い

Last updated at Posted at 2022-03-28

uniqメソッドとは

uniqメソッドとは、Railsで取得した重複レコードを1つにまとめるためのメソッド。
uniqメソッドを実行すると、Railsは内部的にSQLのDISTINCTを実行する。

その他に、uniqメソッドとまったく同じ働きをするdistinctメソッドも存在する。

両者の違いは、

uniq → Rails5以降で非推奨(または使えなくなる)
distinct → Rails5以降で正式メソッド

Image from Gyazo

使い方

モデル.select(カラム).distinct

参考記事

【Rails入門】uniqで重複が無いモデルにしよう!distinctにも対応

0
0
1

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